Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery mouse click counter

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

My question is, how to count 2 clicks?

like image 746
Milan Vučković Avatar asked Sep 07 '12 13:09

Milan Vučković


3 Answers

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

(function () {
  var count = 0;

  $('table').click(function () {
    count += 1;

    if (count == 2) {
      // come code
    }
  });
})();
like image 68
Danil Speransky Avatar answered Oct 21 '22 04:10

Danil Speransky


You can use jQuery's toggleClass function for that:

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

When clicked once, the element has the someClass class, and when clicked twice, the class is removed again.

like image 40
Constantinius Avatar answered Oct 21 '22 02:10

Constantinius


I might be wrong, but in between the lines of your question I read that you actually ask about toggleClass() method documented here.

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

like image 34
Michal Klouda Avatar answered Oct 21 '22 03:10

Michal Klouda