Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel jQuery event handling

I have setup onclick event handler in the following manner:

element.onclick = function() { /*code */ }

Imagine there are event handlers setup using jQuery method bind() or similar handlers.

$('element').bind('click', function(){/*another function*/})

How can I prevent invoking handler defined with jQuery from the handler I have described in the beginning?

NB stopPropagation() and etc. jQuery's methods doesn't work from that function, because it is passed with native event object.

like image 548
glaz666 Avatar asked Jan 29 '09 14:01

glaz666


1 Answers

I'm not 100% sure what you're asking but maybe this will help:

You can create a new event object (compliant with W3C DOM) via jQuery's exposed Event constructor:

For example:

element.onclick = function(e) {
    var aBetterEventObject = jQuery.Event(e);
    // Now you can do what you want: (Cross-browser)
    aBetterEventObject.preventDefault()
    aBetterEventObject.isDefaultPrevented()
    aBetterEventObject.stopPropagation()
    aBetterEventObject.isPropagationStopped()
    aBetterEventObject.stopImmediatePropagation()
    aBetterEventObject.isImmediatePropagationStopped()
}

EDIT: Reading through your question again, I don't think propagation is the problem - you seem to want to cancel an event handler from running within an event handler - I'm not sure this is possible. You could just unbind all handlers (jQuery(elem).unbind('click')) but I don't think that's what you're after...

like image 197
James Avatar answered Oct 08 '22 01:10

James