7

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?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Milan Vučković
  • 99
  • 1
  • 1
  • 7

3 Answers3

14

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

(function () {
  var count = 0;

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

    if (count == 2) {
      // come code
    }
  });
})();
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
3

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.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
2

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.

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77