Run function after multiple clicks

Viewed 56

I was wondering if it is possible to only run a function after a button is clicked multiple times, let's say 3 times. I'm kind of a newby with programming, but if i'm guessing it has to do something with a click count and variables.

Allright, thanks!

2 Answers

var click = 0;

var buttonElement = document.getElementById('id');

buttonElement.addEventListener("click", function(){
click++;
if (click >=2 ){
//run your stuff here
}
})

This code will show "hello world" after 3 clicks!

<button onclick="onClick()">CLICK ME</button>
<div id="output"></div>


<script>
    var clicks = 0;
    function onClick() {
        clicks += 1;
            if (clicks == 3) {
                //run the code you want here
                document.getElementById("output").innerHTML = "Hello, world!";
            }
        };
</script>
Related