Skip to content

Instantly share code, notes, and snippets.

@tx2z
Last active December 14, 2015 23:49
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 tx2z/5168797 to your computer and use it in GitHub Desktop.
Save tx2z/5168797 to your computer and use it in GitHub Desktop.
Capture simple, double, triple, long and right clicks in the same element with jQuery - http://jesus.perezpaz.es/2013/01/capture-simple-double-triple-long-and-right-clicks-in-the-same-element-with-jquery/
(function($) {
// Usefull variables
var clicks = 0;
var longclick = false;
// Change the long click duration (in ms):
//jQuery.longclick.duration = 500;
$(document).ready(function() {
// Event click handlers
$('a').bind({
click: function(){ // Left (normal) click
if (longclick == true) {
longclick = false;
}else {
clicks++;
if (clicks == 1) {
setTimeout(function() {
if (clicks == 1) { // One click
$('.message').html('click');
} else if (clicks == 2) { // Double click
$('.message').html('dblclick');
} else { // Triple click
$('.message').html('tripleclick');
}
clicks = 0;
}, 500);
}
}
},
contextmenu: function(){ // Right click
$('.message').html('left-click');
return false; // The retun false avoid show the context menu
},
longclick: function(){ // Long click
$('.message').html('long click');
longclick = true; // Stop the "normal" click to execute
}
});
});
})(window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment