Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Increment counter each time an element is clicked

// increment counter each time the picture element is clicked
        var counter = 0;//Counter variable starts at 0
        const var_counter = document.querySelector("#out_count");//display the number of clicks
        const elem = document.querySelector("body > div > div.box.kat_kontainer > img");//select the picture
        elem.addEventListener("click", function () {//function to be performed on each click
            counter += 1;//Counter variable is incremented by one
            var_counter.innerHTML = counter;//Display the value of the counter in the display element
        });
 
PREVIOUS NEXT
Tagged: #Increment #counter #time #element #clicked
ADD COMMENT
Topic
Name
1+5 =