I\'m writing event handlers using jQuery, and had a question about the click and double-click events. I\'ve got an element with a class \"file\" on it. It has two event handlers
Dont trust the first answers, thats really really bad. Use this instead:
$('.file').on('click', function()
{
$(this).css('background', 'blue');
});
$('.file').on('dblclick', function(event){
event.preventDefault();
$(this).css('background', 'green');
});
http://jsfiddle.net/H42KV/
This is the default behavior of jquery. You must use/do something like this: https://gist.github.com/399624
As James mentions in his comment, there cannot be one without the other. It's going to be a bit of a hack, but you can have these work independently with a timer:
$('.file').live('dblclick', function() {
$(this).data('doubleClicked', true);
//event handler code
});
function SingleClickHandle(this)
{
if (! $(this).data('doubleClicked'))
{
//event handler code
}
}
$('.file').live('click', function(){
$(this).data('doubleClicked', false);
setTimeout('SingleClickHandler(this)', 500); // is half a second enough to consider it a double-click?
});