Capture, target and bubble phases
Quick answer
Events travel along a dispatch path. Capturing listeners on ancestors run before the target; at the target, capture listeners precede noncapture listeners. If the event bubbles, noncapture ancestor listeners run on the return path. eventPhase identifies the phase; not every event bubbles.
Browser examples
Run each snippet in a fresh browser document. These are static code blocks; ByteMind browser execution is not verified. Storage examples require a test origin.
const p=document.createElement("div"),b=document.createElement("button");p.append(b);p.addEventListener("click",e=>console.log("capture",e.eventPhase),true);b.addEventListener("click",e=>console.log("target",e.eventPhase));p.addEventListener("click",e=>console.log("bubble",e.eventPhase));b.click();Expected console output:
capture 1target 2bubble 3Ancestor capture precedes target and ancestor bubble.
const p=document.createElement("div"),b=document.createElement("button");p.append(b);p.addEventListener("demo",()=>console.log("capture"),true);p.addEventListener("demo",()=>console.log("bubble"));b.dispatchEvent(new Event("demo"));Expected console output:
captureA nonbubbling event still goes through capture.
Common mistake
Capture is not equivalent to stopping propagation. An event with bubbles false may still reach ancestor capture listeners.
Practice
Add a grandparent and label the order of its capture and bubble callbacks.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Capture, target and bubble phases — Knowledge Check
Apply the concept and predict the browser example behavior.
Which phase first reaches ancestor capture listeners?
What is eventPhase at the target?
Can a nonbubbling event reach ancestor capture listeners?
What is the exact console output in Capture, target and bubble phases example 1?
const p=document.createElement("div"),b=document.createElement("button");p.append(b);p.addEventListener("click",e=>console.log("capture",e.eventPhase),true);b.addEventListener("click",e=>console.log("target",e.eventPhase));p.addEventListener("click",e=>console.log("bubble",e.eventPhase));b.click();What is the exact console output in Capture, target and bubble phases example 2?
const p=document.createElement("div"),b=document.createElement("button");p.append(b);p.addEventListener("demo",()=>console.log("capture"),true);p.addEventListener("demo",()=>console.log("bubble"));b.dispatchEvent(new Event("demo"));