liveBook · Manning

Event phase

As explained in Vanilla Web

A stage in an event's traversal through the DOM, consisting of capture, target, or bubble processing.

When a click has listeners on both a paragraph and its ancestors, the event phase determines their order. A DOM event travels in three phases: capture moves down from the window and document toward the target; target reaches the clicked element; bubble moves back up through its ancestors. section 6.2.13

Use addEventListener with { capture: true } to listen on the way down. Listeners use the bubble phase by default. At the target, both capture and bubble listeners can run. Choose the listener’s node and phase according to when it should respond; stopPropagation() halts the remaining path, while stopImmediatePropagation() also skips remaining listeners on the same element. section 6.2.13

Figure 6.2 A diagram of a DOM and what happens when the user clicks a paragraph element. The event first travels down in the capture phase, reaches the target, then travels back up in the bubble phase. Several listeners can react to the same event, executing in order across the phases.
ch06 F002 firtman

Follow the click from the document down to the paragraph, then back through its ancestors. section 6.2.13

How Vanilla Web covers it

How deep it goes
Explains capture, target, and bubble order, listener defaults, and propagation controls through a click example.
Examples
Traces a paragraph click through document, body, header, and paragraph listeners in both phases.
What it assumes
Assumes familiarity with DOM elements and click listeners; defines the three phases directly.
What you can do after
Apply it via addEventListener to choose listener phase and anticipate callback order.
How it connects
Connects DOM events to addEventListener, event propagation, stopPropagation(), and stopImmediatePropagation().

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.