-
-
Save ashirazi/944651 to your computer and use it in GitHub Desktop.
Treat multiple clicks as single-click in browsers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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