简体   繁体   中英

How to make a function only fire when 3 buttons are clicked in Vanilla Javascript?

I couldn't figure this out or find anything on this on the web. Basically how would you write code for this: I have 3 buttons, and I need to click all of them before the function fires. I know how to have each button do the same thing, but I want it so I have to click all 3 for it to do anything.

If you could give me any example code of this I can find a way to implement it into my project.

If you need me to elaborate let me know. Thanks!

Use a variable to store click counters

 let clicks = 0; function myFunction() { if (clicks < 2) { clicks += 1; return } document.getElementById("demo").innerHTML = "Hello, after 3 clicks"; }
 <p id="demo"></p> <button onclick="myFunction()">Button 1</button> <button onclick="myFunction()">Button 2</button> <button onclick="myFunction()">Button 3</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM