+ 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.

8th Feb 2020, 8:18 AM
Ginfio
Ginfio - avatar
17 Respostas
+ 6
setTimeout( yourClickCallback, 1000);
8th Feb 2020, 8:26 AM
Gordon
Gordon - avatar
+ 3
Oma Falk hmm... like... func() function func(){ //do something.. } like that?
8th Feb 2020, 8:24 AM
Ginfio
Ginfio - avatar
+ 3
You could make a function triggered by click event, why can't you call the function directly?
8th Feb 2020, 8:34 AM
Calviղ
Calviղ - avatar
+ 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.
8th Feb 2020, 9:09 AM
Calviղ
Calviղ - avatar
+ 3
This may be helpful. https://youtu.be/xHGPBX5_lhw
8th Feb 2020, 11:09 AM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar
+ 3
ok, i got it: button.click()
10th Feb 2020, 8:36 PM
Ginfio
Ginfio - avatar
+ 2
you can call the onClick function directly
8th Feb 2020, 8:22 AM
Oma Falk
Oma Falk - avatar
+ 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 ...
8th Feb 2020, 8:39 AM
Ginfio
Ginfio - avatar
+ 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?
9th Feb 2020, 2:45 PM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar
+ 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.
9th Feb 2020, 2:49 PM
XXX
XXX - avatar
+ 2
XXX , 🇮🇳Vivek🇮🇳 I tried it on console, through inspect, and it worked.
9th Feb 2020, 3:37 PM
Ginfio
Ginfio - avatar
+ 1
Hack Net
8th Feb 2020, 7:04 PM
Arterg
+ 1
I don't know about pure JavaScript, but using jQuery allows you to use $("#"+(Button-id)).trigger("click")
9th Feb 2020, 2:29 PM
XXX
XXX - avatar
+ 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
10th Feb 2020, 6:29 AM
Anmol Kumar
Anmol Kumar - avatar
0
Hack Net
8th Feb 2020, 7:04 PM
Arterg
0
Free Internet
8th Feb 2020, 7:04 PM
Arterg