Skip to content

Instantly share code, notes, and snippets.

@ashirazi
Forked from bokmann/README
Last active September 25, 2015 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashirazi/944651 to your computer and use it in GitHub Desktop.
Save ashirazi/944651 to your computer and use it in GitHub Desktop.
Treat multiple clicks as single-click in browsers
Code
// Treat double-click like a single-click
$("button").multi_click(function () {
alert("It's all good.")
})
// Different actions for single- or double-click
$("button").multi_click(function () {
alert("Try double-clicking me!")
}, function () {
alert("Double click detected, I'm hiding")
$(this).hide()
})
Markup
<button>Click Me!</button>
// Author: Jacek Becela
// Modifications: Arild
// Source: http://gist.github.com/399624
// License: MIT
jQuery.fn.multi_click = function(on_single_click, on_double_click, timeout) {
return this.each(function(){
var clicks = 0, self = this;
on_double_click = on_double_click || on_single_click
jQuery(this).click(function(event){
clicks++;
if (clicks == 1) {
setTimeout(function(){
if(clicks == 1) {
on_single_click.call(self, event);
} else {
on_double_click.call(self, event);
}
clicks = 0;
}, timeout || 500);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment