jQuery equivalent of JavaScript's addEventListener method
Solution 1:
Not all browsers support event capturing (for example, Internet Explorer versions less than 9 don't) but all do support event bubbling, which is why it is the phase used to bind handlers to events in all cross-browser abstractions, jQuery's included.
The nearest to what you are looking for in jQuery is using bind()
(superseded by on()
in jQuery 1.7+) or the event-specific jQuery methods (in this case, click()
, which calls bind()
internally anyway). All use the bubbling phase of a raised event.
Solution 2:
As of jQuery 1.7, .on()
is now the preferred method of binding events, rather than .bind()
:
From http://api.jquery.com/bind/:
As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to .bind() occurs. For more flexible event binding, see the discussion of event delegation in .on() or .delegate().
The documentation page is located at http://api.jquery.com/on/
Solution 3:
The closest thing would be the bind function:
http://api.jquery.com/bind/
$('#foo').bind('click', function() {
alert('User clicked on "foo."');
});