augmented clone/fork of AlloVince's fixclick plugin - his site is down so this is based on the v1.0 floating around...
-
http://api.jquery.com/dblclick/
Also pay attention to the note near the end where it says:
It is inadvisable to bind handlers to both the click and dblclick events for the same element. [...]
-
http://stackoverflow.com/questions/6330431/jquery-bind-double-click-and-single-click-separately
When you bind both the single click and double click (dblclick) event on a DOM element, the click handler will fire once (or twice) before the dblclick handler is invoked.
With this plugin you can safely bind to both click and double click (dblclick) events and only have the appropriate one execute when the user clicks/double-clicks.
Note that the single-click event will be delayed by about 300 ms. This is unfortunately necessary to discern between both events.
Example usage:
$("#test").fixClick(function (e) {
// do something here when click
//
// Note: this handler will be delayed!
// ($.fn.fixClick.clickDelay = 300 milliseconds by default)
//
// `e` event object is a clone of the original event and has
// its `.simulated` field set to boolean `true`.
...
}, function (e) {
// do something here when dblclick
//
// `e` is the usual event object you'ld expect for any jQuery
// dblclick action.
...
});
Enhanced example usage:
$("#test").fixClick(function (e) {
// do something here when click
//
// Note: this handler will be delayed!
// ($.fn.fixClick.clickDelay = 300 milliseconds by default)
//
// `e` event object is a clone of the original event and has
// its `.simulated` field set to boolean `true`.
...
}, function (e) {
// do something here when dblclick
//
// `e` is the usual event object you'ld expect for any jQuery
// dblclick action.
...
}, function (e) {
// this optional handler gets invoked *immediately* when
// a click event arrives. Here you may perform some app-specific
// setup/signaling.
//
// Warning/Note: this handler is, of course, invoked for each
// click event, hence it will also be invoked for a dblclick.
// When you wish jQuery-FixClick to *not* set up and fire a
// delayed click handler (the first argument of this fixClick()
// call), you can accomplish this by invoking
// `e.stopImmediatePropagation()`.
...
if (some_condition_where_you_dont_want_the_delayed_click_called) {
e.stopImmediatePropagation();
}
});