unomi
unomi

Reputation: 2672

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

Upvotes: 7

Views: 4871

Answers (3)

MiB
MiB

Reputation: 480

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.

Upvotes: 1

jackrugile
jackrugile

Reputation: 1663

Hey, I think this is what you are trying to do with your code. If not, I apologize, I may have misunderstood the question. I used jQuery to get it done: http://jsfiddle.net/jackrugile/KArRD/

$('a').bind({
    mousedown: function(){
        // Do stuff
    },
    click: function(e){
        e.preventDefault();
    }
});

Upvotes: 0

Premature Optimization
Premature Optimization

Reputation: 1938

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/

Upvotes: 3

Related Questions