Can someone explain me why is there a difference in the order in which the event handlers are being executed depending on how they have been attached? In the example below I'm using the .on()
and .addEventListener()
methods to handle a specific event on different DOM
elements.
jsfiddle: http://jsfiddle.net/etsS2/
I thought that in this particular example the order in which the event handlers are going to be executed will depend on the event-bubbling
- so starting from the original event target
and moving up to the document
element.
document.getElementById('outer').addEventListener('mouseup', function (event) { //$('#outer').on('mouseup', function (event) { alert('This alert should not show up!'); }, false);
If I uncomment the on();
version everything works as expected - is there a difference in how jQuery
handles events contrary to plain JavaScript
?
addEventListener can add multiple events to a particular element. onclick can add only a single event to an element. It is basically a property, so gets overwritten. addEventListener can take a third argument that can control the event propagation.
Both can be used to handle events. However, addEventListener should be the preferred choice since it can do everything onclick does and more. Don't use inline onclick as HTML attributes as this mixes up the javascript and the HTML which is a bad practice. It makes the code less maintainable.
The addEventListener() method attaches an event handler to an element without overwriting existing event handlers. You can add many event handlers to one element. You can add many event handlers of the same type to one element, i.e two "click" events.
The addEventListener() is an inbuilt function in JavaScript which takes the event to listen for, and a second argument to be called whenever the described event gets fired. Any number of event handlers can be added to a single element without overwriting existing event handlers.
When you use .on()
at the document level, you're waiting for the event to bubble all the way up to that point. The event handler for any intermediate container will already have been called.
Event "bubbling" is the process by which the browser looks for event handlers registered with parents of the element that was the actual original recipient of the event. It works upwards in the DOM tree. The document level is the last level checked. Thus, your handler registered with .on()
won't run until that level is reached. In the meantime, the other event handler at the "outer" level is reached first and it is executed by the browser.
Thus, that return false;
in the handler registered with .on()
is pretty much useless, as would be a call to event.stopPropagation()
. Beyond that, mixing native event handler registration with the work that a library like jQuery will do is probably a really bad idea unless you seriously know what you're doing.
There was a question asked almost exactly like this one just a little while ago today.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With