+6

How do I auto click a button in js?

How can I get a button to click itself, I mean ... can I? Like maybe if a have a button that would change the background color of something, and I want it to be clicked in one second, without me tapping / clicking it.,- I thought button.clicked == true would work, it doesn’t seem to be working.

2/8/2020 8:18:51 AM

Ginfio

18 Answers

New Answer

+4

setTimeout( yourClickCallback, 1000);

+3

Oma Falk hmm... like... func() function func(){ //do something.. } like that?

+3

You could make a function triggered by click event, why can't you call the function directly?

+3

Ginfio there is no property called clicked on button element. If you set the click event button.onclick = onRock; function onRock() { alert('rock selected'); } You can also call the click event callback, by onRock(); If you want to trigger state, you should use checkbox input, not button.

+3

This may be helpful. https://youtu.be/xHGPBX5_lhw

+3

Answer: button.click(); BUT instead of doing this: setTimeout(button.click(), 1000); Just call that function directly: setTimeout(changeColorFunction, 1000)

+3

ok, i got it: button.click()

+2

you can call the onClick function directly

+2

Calviղ I guess i could. I’m making a rock paper sccisors game and, it wasn’t a big problem, but something there was something that bothered me about it, so my first thought and solution was: when that button is clicked, this button.clicked = true. But, let me think about it a little bit more, and see if calling the function is just the correct sulution ...

+2

XXX I think. It is bad idea to use library for just a method. We can simply use button.click() method. Have you try?

+2

🇮🇳Vivek🇮🇳 I haven't. I told you that I don't know about pure JavaScript. So if he was using jQuery he could have used the trigger method. Thanks for the info.

+2

XXX , 🇮🇳Vivek🇮🇳 I tried it on console, through inspect, and it worked.

+1

Hack Net

+1

Free Internet

+1

I don't know about pure JavaScript, but using jQuery allows you to use $("#"+(Button-id)).trigger("click")

+1

If you want your button get auto clicked, then you should use window.load method , I think that would be the same as button get auto clicked

0

Hack Net

0

I dont know why but you can click your button with jQuery: $("#myButton").click();