Are event handlers in JavaScript called in order?

12,507

This has been changed with DOM3! While the DOM level 2 events specification did state

When the event reaches the target, any event listeners registered on the EventTarget are triggered. Although all EventListeners on the EventTarget are guaranteed to be triggered by any event which is received by that EventTarget, no specification is made as to the order in which they will receive the event with regards to the other EventListeners on the EventTarget.

The current DOM level 3 events specification does now state

The implementation MUST determine the current target's candidate event listeners. This MUST be the list of all event listeners that have been registered on the current target in their order of registration. HTML5 defines the ordering of listeners registered through event handler attributes. […]

Finally, the implementation MUST process all candidate event handlers in order […]

However, I can't find a reference to this behaviour in the DOM 4 draft any more.

Share:
12,507

Related videos on Youtube

Sasha Chedygov
Author by

Sasha Chedygov

Stuff I'm great at: Frontend JavaScript (esp. React), Python, Node.js, HTML/CSS Stuff I'm good at: SQL, UX Stuff I'm learning: Rust, cloud infrastructure stuff

Updated on September 22, 2020

Comments

  • Sasha Chedygov
    Sasha Chedygov over 3 years

    If I attach multiple event handlers to a single event on a single DOM element, are the event handlers guaranteed to be called in the order they were added? Or should I not rely on this behavior?

  • thomasrutter
    thomasrutter almost 8 years
    Presumably if this comes from HTML5 it means that current browsers already do this, do you think?
  • Bergi
    Bergi almost 8 years
    @thomasrutter: Yes, pretty much - they only standardise what they implement.
  • Tomasz P. Szynalski
    Tomasz P. Szynalski over 4 years
    The answer appears to be out of date. The current DOM Level 3 spec doesn't include the quoted paragraph about candidate event listeners anymore, unfortunately.