Learn JavaScript
❯ browser_dom.delegation

Bubbling, Capturing & Delegation in JavaScript

$concept

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.

$how it works
1

Down, at, up

Capture descends window→target, bubble ascends target→window — same event observed at every level along the path.

2

Phase selects direction

Default listeners bubble; { capture: true } intercepts descending — interception vs reaction.

3

Delegate from parents

One parent listener plus e.target.closest("li") serves all current and future children — dynamic lists solved.

4

Stop deliberately

stopPropagation halts travel (modals, nested widgets); overuse breaks delegated ancestors — stop narrowly.

$example
document.querySelector("#list").addEventListener("click", (e) => {
let item = e.target.closest("li");
if (item) console.log("picked", item.textContent);
});
Output
(clicking any current or future item logs: picked <text>)
1One listener on the list catches clicks bubbling from any item — present or added later.
2closest("li") finds the originating item (or null for gaps, guarded).
3Dynamic children work with zero additional subscriptions — delegation's payoff.
$use cases
Dynamic lists and tables with one delegated listener
Modal dismissal via backdrop-vs-content travel reasoning
Capture-phase interception for global shortcuts
closest() guards distinguishing items from container gaps

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.