196

I'm trying to find the jQuery equivalent of this JavaScript method call:

document.addEventListener('click', select_element, true);

I've gotten as far as:

$(document).click(select_element);

but that doesn't achieve the same result, as the last parameter of the JavaScript method - a boolean that indicates whether the event handler should be executed in the capturing or bubbling phase (per my understanding from http://www.quirksmode.org/js/events_advanced.html) - is left out.

How do I specify that parameter, or otherwise achieve the same functionality, using jQuery?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Bungle
  • 17,944
  • 23
  • 75
  • 101
  • 3
    Event capturing is not supported by jQuery, as event capturing is not supported by IE, which jQuery supports ;) Are you looking for IE compatibility? – Crescent Fresh Mar 07 '10 at 21:57
  • Thanks, Crescent Fresh - I think that makes sense now. I do need IE compatibility so I suppose I need to forget about the capturing phase. – Bungle Mar 07 '10 at 22:31

7 Answers7

147

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.

Russ Cam
  • 117,566
  • 29
  • 193
  • 253
  • 6
    Looks like IE9 finally supports it. http://blogs.msdn.com/b/ie/archive/2010/03/26/dom-level-3-events-support-in-ie9.aspx – some Jul 27 '12 at 20:52
  • and why they dont support event capturing ? what are the downsides of event capturing ? – Aakash Sep 09 '15 at 16:34
  • 2
    Just to be clear, you're saying that what the OP wants is not possible -- that you have to use bubbling, and cannot use capturing. Right? – Noumenon Apr 23 '17 at 02:47
117

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/

54

The closest thing would be the bind function:

http://api.jquery.com/bind/

$('#foo').bind('click', function() {
  alert('User clicked on "foo."');
});
Ben Rowe
  • 26,794
  • 6
  • 50
  • 72
14

One thing to note is that jQuery event methods do not fire/trap load on embed tags that contain SVG DOM which loads as a separate document in the embed tag. The only way I found to trap a load event on these were to use raw JavaScript.

This will not work (I've tried on/bind/load methods):

$img.on('load', function () {
    console.log('FOO!');
});

However, this works:

$img[0].addEventListener('load', function () {
    console.log('FOO!');
}, false);
Kyll
  • 6,830
  • 6
  • 39
  • 56
tbjers
  • 547
  • 3
  • 13
13

You should now use the .on() function to bind events.

6

$( "button" ).on( "click", function(event) {

    alert( $( this ).html() );
    console.log( event.target );

} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<button>test 1</button>
<button>test 2</button>
antelove
  • 2,039
  • 16
  • 14
1

Here is an excellent treatment on the Mozilla Development Network (MDN) of this issue for standard JavaScript (if you do not wish to rely on jQuery or understand it better in general):

https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener

Here is a discussion of event flow from a link in the above treatment:

http://www.w3.org/TR/DOM-Level-3-Events/#event-flow

Some key points are:

  • It allows adding more than a single handler for an event
  • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
  • It works on any DOM element, not just HTML elements
  • The value of "this" passed to the event is not the global object (window), but the element from which the element is fired. This is very convenient.
  • Code for legacy IE browsers is simple and included under the heading "Legacy Internet Explorer and attachEvent"
  • You can include parameters if you enclose the handler in an anonymous function
Xitalogy
  • 1,516
  • 1
  • 14
  • 15