Justin Grant
Justin Grant

Reputation: 46763

How to tell if a mouseup is going to be followed by a click event?

Is there any way to know, in a jQuery onmouseup handler, if the event is going to be followed by a click event for the same element?

I have an event handler for a menu hyperlink which unbinds itself when the user either clicks on an element or "drops" (as in drag-n-drop) on an element. I want to avoid prematurely unbinding the handler on mouseup if a click is coming next.

I realize I can track mousedown and mouseup events myself or otherwise hack up a solution (e.g. wait 50 msecs to see if a click comes soon), but I was hoping to avoid rolling my own implementation if there's something built-in for this purpose.

Upvotes: 3

Views: 511

Answers (2)

Nicolas Zozol
Nicolas Zozol

Reputation: 7048

There is nothing built-in because it's really specific to your needs. Thus, there would kilometers of code and documentation to maintain if jQuery would handle any combination of clicks, long clicks, moves, etc.

It's also hard to give you a snippet that satisfies your needs, but a setTimeout is usually the first step to take, with something like that :

obj.mouseup = function (){
   obj.click = action; // do action 
   setTimeout ( function() {
       obj.click = functionOrigin // after 500 ms, disable the click interception
   }, 500);
};

Upvotes: 3

quzary
quzary

Reputation: 285

you can use $(selector).data('events') for that

$('div').mouseup(function(){
 if($(this).data('events').click){
  console.log('Has a click event handler')
 }
});

Upvotes: 2

Related Questions