3

I am trying to understand what true or false does when adding addEventListener in Internet Explorer. According to Microsoft it is useCapture. If I add an event such as:

element.addEventListener('click', function_name, true);

It does not appear to change anything, the listener still works. Can anyone explain the purpose of useCapture parameter please?

Ian
  • 50,146
  • 13
  • 101
  • 111

1 Answers1

13

It is the optional useCapture parameter that specifies the event phase to add the event handler for:

Events are handled in two phases: capturing and bubbling. During the capturing phase, events are dispatched to parent objects before they are dispatched to event targets that are lower in the object hierarchy. During the bubbling phase, events are dispatched to target elements first and then to parent elements. You can register event handlers for either event phase.

true Register the event handler for the capturing phase.

false Register the event handler for the bubbling phase.

You can read the eventPhase documentation here: http://msdn.microsoft.com/en-gb/library/ie/ff974944(v=vs.85).aspx

EDIT:

Please read the following that describes the event order defined by bubbling and capturing with clear examples. http://www.quirksmode.org/js/events_order.html

Fraser
  • 15,275
  • 8
  • 53
  • 104
  • 1
    Hi,Thanks for you reply, but that really does not help me at all. I have no idea what it means. guess I just not care and use true seems to work, thanks for your reply. – Wayneuser1168636 Dec 20 '12 at 06:58
  • 2
    I'm not sure what you mean. If you set the `useCapture` to true then the event uses the capturing phase, if false (default) it uses the bubbling phase. What this means is clearly defined in the link I provided...Anyhow, I have added another link that explains more clearly. Basically it is the execution order in which events are fired. – Fraser Dec 20 '12 at 09:13
  • @Wayneuser1168636 see the example in this reply, it helped me understanding: http://stackoverflow.com/questions/7398290/unable-to-understand-usecapture-attribute-in-addeventlistener#answer-7398447 – gal007 Apr 19 '17 at 20:19