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.

Community
  • 1
  • 1
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

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,313
  • 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
  • 11,966
  • 13
  • 49
  • 68
  • 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