Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering jQuery click, first and second click

Is there a way to run two functions similar to this:

$('.myClass').click(
    function() {
        // First click
    },
    function() {
        // Second click
    }
);

I want to use a basic toggle event, but .toggle() has been deprecated.


2 Answers

Try this:

$('.myClass').click(function() {
  var clicks = $(this).data('clicks');
  if (clicks) {
     // odd clicks
  } else {
     // even clicks
  }
  $(this).data("clicks", !clicks);
});

This is based on an already answered question: Alternative to jQuery's .toggle() method that supports eventData?

like image 112
henser Avatar answered Sep 12 '25 11:09

henser


Or this :

var clicks = 0;

$('.myClass').click(function() {
    if (clicks == 0){
        // first click
    } else{
        // second click
    }
    ++clicks;
});
like image 20
Stevens Avatar answered Sep 12 '25 11:09

Stevens