Bubbling, Capturing & Delegation in JavaScript
Events travel in three phases: capturing (window down to target), target, then bubbling (target back up to window). Listeners default to bubble phase; { capture: true } catches the way down. stopPropagation halts travel; stopImmediatePropagation also blocks same-node siblings.
Delegation exploits bubbling: one listener on a parent handles infinite dynamic children via e.target matching (list.addEventListener("click", e => e.target.closest("li") && ...)) — no per-item subscriptions, automatic coverage for added nodes, and single cleanup point. It's the standard pattern for dynamic lists.
Down, at, up
Capture descends window→target, bubble ascends target→window — same event observed at every level along the path.
Phase selects direction
Default listeners bubble; { capture: true } intercepts descending — interception vs reaction.
Delegate from parents
One parent listener plus e.target.closest("li") serves all current and future children — dynamic lists solved.
Stop deliberately
stopPropagation halts travel (modals, nested widgets); overuse breaks delegated ancestors — stop narrowly.
document.querySelector("#list").addEventListener("click", (e) => { let item = e.target.closest("li"); if (item) console.log("picked", item.textContent);});Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.