jQuery .on(); vs JavaScript .addEventListener();

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.

Leave a Comment