Alternative to jQuery’s .toggle() method that supports eventData?

Seems like a reasonable way to do it… I’d just suggest that you make use of jQuery’s data storage utilities rather than introducing an extra variable (which could become a headache if you wanted to keep track of a whole bunch of links). So based of your example:

$('a').click(function() {
  var clicks = $(this).data('clicks');
  if (clicks) {
    alert('odd number of clicks');
  } else {
    alert('even number of clicks');
  }
  $(this).data("clicks", !clicks);
});

Leave a Comment