Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to cancel click/mouseup events when double-click event detected

How is this done?

like image 715
Daddy Warbox Avatar asked Jul 01 '09 05:07

Daddy Warbox


People also ask

How do you stop a single click event when double clicking?

on('click',function(e){ if(e. originalEvent. detail > 1){ return; /* if you are returning a value from this function then return false or cancel the event some other way */ } }); Done.

How do I get rid of mouseup event?

If you wish suppress the click event you have add the statement event. preventDefault() in the click event handler only and not the mouseup event handler.

Does mouseup fire before click?

Mouseup is always firing before click, despite this order.

How do I stop a website from double clicking?

If you want to disable any mouse click action use addEventListener(event, function, useCapture) . Onclick ,call this function.


2 Answers

This is a good question, and I actually don't think it can be done easily. (Some discussion on this)

If it is super duper important for you to have this functionality, you could hack it like so:

function singleClick(e) {     // do something, "this" will be the DOM element }  function doubleClick(e) {     // do something, "this" will be the DOM element }  $(selector).click(function(e) {     var that = this;     setTimeout(function() {         var dblclick = parseInt($(that).data('double'), 10);         if (dblclick > 0) {             $(that).data('double', dblclick-1);         } else {             singleClick.call(that, e);         }     }, 300); }).dblclick(function(e) {     $(this).data('double', 2);     doubleClick.call(this, e); }); 

And here is an example of it at work.

As pointed out in the comments, there is a plugin for this that does what I did above pretty much, but packages it up for you so you don't have to see the ugly: FixClick.

like image 115
Paolo Bergantino Avatar answered Sep 20 '22 01:09

Paolo Bergantino


Raymond Chen has discussed some of the implications of single-versus-double clicking - although he's talking in the context of Windows, what he says is relevant to browser-based UI design.

Basically, the action taken on a double click should be a logical thing to do after a single click. So for example, in a desktop UI, single click selects an item, and double click opens it (e.g. opens the file, or launches the application). The user would have to select the file to open it anyway, so it doesn't matter that the single click action is taken before the double click action.

If you have a UI component whose double click action is completely unrelated to the single click action, such that it becomes necessary to prevent the single click action from occurring once the system realises it was actually a double click, then you really ought to rethink your design. Users will find it awkward and counter-intuitive, in that it will not act in the way they are used to things acting.

If you still want to go that way, then you will either have to use the debouncing technique (in which case all single click actions will be delayed) or else implement some mechanism whereby the double click handler undoes the work done by the single click handler.

You should also be aware that some users set a very long double click time. Somebody with, for example, arthritic hands might have a double click time of more than a second set in their system preferences, so the debouncing technique based on some arbitrary time period of your choosing is going to make your UI component inaccessible to those people if taking the single click action precludes taking the double click action. The "undo what just happened on single click" technique is the only viable workaround for this, as far as I know.

like image 23
NickFitz Avatar answered Sep 17 '22 01:09

NickFitz