liveBook · Manning

Bubble phase

As explained in Vanilla Web

The event-propagation stage in which an event travels from its target back through ancestor elements.

A click on a nested element can trigger listeners on its ancestors. DOM events travel through three phases: capture moves down the tree, the target phase reaches the clicked element, and the bubble phase carries the event back up through its ancestors toward the document and window. Listeners use the bubble phase by default; request capture explicitly when you need the downward path. section 6.2.13

const header = document.querySelector("header");
const p = document.querySelector("p");

// Capture phase: fires top-down, from document to the target
document.addEventListener("click", () =>  #1
  console.log("1 document capture"), { capture: true });
document.body.addEventListener("click", () =>
  console.log("2 body capture"), { capture: true });
header.addEventListener("click", () =>
  console.log("3 header capture"), { capture: true });
p.addEventListener("click", () =>
  console.log("4 p capture"), { capture: true });

// Bubble phase: fires bottom-up, from the target to document
p.addEventListener("click", () =>  #2
  console.log("5 p bubble"));
header.addEventListener("click", () =>
  console.log("6 header bubble"));
document.body.addEventListener("click", () =>
  console.log("7 body bubble"));
document.addEventListener("click", () =>
  console.log("8 document bubble"));

Compare the logged order for capture listeners with the bottom-up order for bubble listeners when the paragraph is clicked. section 6.2.13

A list-level click listener

Attach one click listener to a list and click a list item. The event’s target identifies the clicked element, while currentTarget identifies the list running the listener. An ancestor listener can therefore react without being attached to each item. section 6.2.13

Use addEventListener when multiple handlers or listener options are needed. CustomEvent can also be dispatched with bubbles: true so an ancestor listener can receive it. section 6.2.13

How Vanilla Web covers it

How deep it goes
Explains propagation order and default listener phase through click-handler code, without descending into browser internals.
Examples
Uses a paragraph click, a list-level listener, and a bubbling custom event.
What it assumes
Assumes basic familiarity with DOM elements and click handlers while defining the three event phases.
What you can do after
Apply it via addEventListener to place ancestor click handlers and distinguish the clicked target from the listener’s element.
How it connects
Connects bubble phase to capture phase, target phase, addEventListener, event targets, and CustomEvent.

Also explained in

Compare how 2 books explain bubble phase →

Related concepts

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.