JQuery mouse click counter

I need to color the table in a zebra style, and then when I click on the table twice (don't double click), it should go back to the original.

My question is: how to count 2 clicks?

+4
source share
3 answers

Demo: http://jsfiddle.net/aztVY/

(function () { var count = 0; $('table').click(function () { count += 1; if (count == 2) { // come code } }); })(); 
+10
source

Maybe I'm wrong, but between the lines of your question, I read that you are really asking about the toggleClass() method registered here .

Add or remove one or more classes from each element in the set of matched elements, depending on the presence of the class or the value of the switch argument.

+2
source

You can use the jQuery toggleClass function to do this:

 $(" ... ").click(function() { $(this).toggleClass("someClass"); }); 

When you click once, the element has the class someClass , and when you double-click, the class is deleted again.

+2
source

Source: https://habr.com/ru/post/1432951/


All Articles