6

If I subscribe dblclick() and click() for a DIV element and make double click by mouse on the DIV, the click() handler calls first before dblclick() is called. How to avoid handling click() before the dblclick()?

UPDATE: The question has already asked before Prevent click event from firing when dblclick event fires. But here are good answers too.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
despero
  • 187
  • 1
  • 10
  • Not only due to this problem it usually is bad GUI design to have both a click and double-click on the same element, unless you want the click action also happen when double clicked. – RoToRa Aug 26 '10 at 13:23
  • Does this answer your question? [Prevent click event from firing when dblclick event fires](https://stackoverflow.com/questions/880608/prevent-click-event-from-firing-when-dblclick-event-fires) – Brian Tompsett - 汤莱恩 Dec 18 '22 at 12:58

2 Answers2

3

Try this: http://jsfiddle.net/DAgyC/ It implements an timeout after the first click, if there is a double-click, the first click is ignored.

chriszero
  • 1,311
  • 3
  • 13
  • 26
2

Probably not the best way to do it: You could set a timeout in the click handler function and clear the timeout in the doubleclick handler function.

The timeout could call a function that will impliment what you really want to happen onClick and that way you can have your dblclick() function handle what happens when the users double clicks.

theycallmemorty
  • 12,515
  • 14
  • 51
  • 71
  • Agreed, this is the only way to do this. OP should note that the jQuery docs mention that it's inadvisable to bind click and dblclick handlers to a single element. http://api.jquery.com/dblclick/ – Aaron Aug 26 '10 at 13:17