Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the mousedown event, is it possible to cancel the resulting click event? or pass info to it?

for arcane reasons I need to be able to cancel the click event via the mousedown event.

Briefly; I am creating a context menu in the mousedown event, however, when the user clicks on the page the context menu should disappear.

I am not able to use the mousedown event over the click in that scenario as I want the user to be able to click links inside the menu ( a full click would never travel to the <a> based menu elements ).

If it is any help, jQuery can be applied.

I would like to either be able to prevent the click event from happening from within the initial mousedown, or be able to pass information to the click event (via originalEvent or otherwise).

TIA

like image 227
unomi Avatar asked May 18 '11 16:05

unomi


2 Answers

Seems to be impossible, neither FF nor Opera didnt cancel upcoming click when prevented in mousedown and/or mouseup (as side note: click is dispatched after mouseup if certain conditions met). testcase: http://jsfiddle.net/ksaeU/

like image 197
Premature Optimization Avatar answered Nov 10 '22 17:11

Premature Optimization


I have just had the exact same problem. I fixed my context menu by closing it on mousedown and eating the mousedown event on the menu so that I can still receive clicks on the menu, like so:

$(document).one('mousedown.ct', null, function() { cmenu.hide(); return false; });
cmenu.bind('mousedown', function(e) { e.stopImmediatePropagation(); });

And in the hide() function I unbind the mousedown.ct again, in case it was closed due to a click on an item.

like image 23
MiB Avatar answered Nov 10 '22 17:11

MiB