Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see if you're clicking the same element twice? Jquery

How can you detect if the user is clicking the same div?

I've tried this with no success:

    _oldthis = null;

    var _this = $(this);

    if(_oldthis == _this) {
        alert('You clicked this last');
    }

    _oldthis = _this;
like image 583
TaylorMac Avatar asked Jul 14 '11 04:07

TaylorMac


2 Answers

You can't compare jQuery objects, but you can compare the DOM objects they contain. Try this:

var previousTarget=null;
$("a").click(function() {
    if(this===previousTarget) {
        alert("You've clicked this element twice.");
    }
    previousTarget=this;
    return false;
});
like image 91
icktoofay Avatar answered Dec 01 '22 00:12

icktoofay


You may try this,

var _oldthis = null;
$('div').click(function(){
  var _this = $(this);

  if(_this == _oldthis) {
    alert('You clicked this last');
    _oldthis = null;
    return false;
  }
  _oldthis = _this;

});

like image 24
Atiqul Alam Avatar answered Nov 30 '22 23:11

Atiqul Alam