views:

2410

answers:

2

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?

+1  A: 

The closest thing would be the bind function:

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

$('#foo').bind('click', function() {
  alert('User clicked on "foo."');
});
Ben Rowe
Thanks, Ben! I appreciate it.
Bungle
+2  A: 

Not all browsers support event capturing (for example, Internet Explorer doesn'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() or the event-specific jQuery method (in this case, click(), which calls bind() internally anyway). Both use the bubbling phase of a raised event.

Russ Cam
Thanks, Russ - that's a good explanation.
Bungle