Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having both mousedown/mouseup and dblclick in jQuery

Tags:

jquery

events

Whenever a mousedown or mouseup handler is attached to an element the dblclick cannot be attached (won't work if attached), though this seems fair enough is there any way to reinstate a dblclick functionality without rewriting it from scratch (sigh...) Or am I missing something about events propagation?

like image 925
Favonius Avatar asked Jan 29 '10 11:01

Favonius


2 Answers

It works - place this code in Firebug on this very page and you'll see it working (try double clicking on the text of your question):

($('.post-text')
    .mousedown(function () { console.log('down'); })
    .mouseup(function () { console.log('up'); })
    .dblclick(function () { console.log('dbclick'); }));

Don't have Firebug? Go grab it, I'll wait!

like image 176
Emil Ivanov Avatar answered Sep 18 '22 11:09

Emil Ivanov


The event.detail parameter will count how many consecutive clicks you had so its possible to use that to detect that a double click has happened.

$('#mybutton').on('click',function(e) {
  console.log("User left clicked!"); // do select code
  if (e.button == 0 && e.detail == 2) {
    console.log("User left doubleclicked!"); // do action code
  }
});
like image 37
Johncl Avatar answered Sep 19 '22 11:09

Johncl