The DOM Event Flow
Build an accurate mental model of what the browser does, from navigation and HTTP through parsing, rendering, storage, workers, security, and DevTools, so you can predict behavior and debug real pages.
What you will be able to do
#- Describe capture, target, and bubble phases.
- Use event delegation correctly.
- Choose listener options such as passive and once.
Overview
#Events travel through the tree in three phases.
Phases
- Capture: from the window down to the target's parent.
- Target: at the target element.
- Bubble: from the target back up to the window.
addEventListener(type, handler, { capture: true }) listens during capture. The default listens during bubble.
Delegation
Attach one listener to a container and inspect event.target instead of adding a listener to every child. Delegation survives re-rendering of children and uses less memory.
Options that matter
once: trueremoves the listener after the first call.passive: truepromises not to callpreventDefault, letting scroll run on the compositor.signallets anAbortControllerremove the listener.
When not to delegate
Events that do not bubble, such as focus and blur, need capture or the focusin and focusout variants.
Questions to carry forward
- Which events do not bubble?
- Why does passive improve scroll performance?
- How does AbortController clean up listeners?
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What does this log when the button is clicked?
starter.javascriptcontainer.addEventListener('click', () => console.log('capture'), true); button.addEventListener('click', () => console.log('target')); container.addEventListener('click', () => console.log('bubble'));Reveal the reference answer
reference.javascriptcapture, target, bubbleAuthored expected output: capture target bubble
The capture listener on the container runs during the capture phase, then the target listener, then the bubble listener on the container.
Exercise 2Implement
Implement click handling for a list of items using delegation and a data-id attribute.
starter.javascriptlist.addEventListener('click', (event) => { // TODO });Reveal the reference answer
reference.javascriptlist.addEventListener('click', (event) => { const item = event.target.closest('[data-id]'); if (!item) return; handle(item.dataset.id); });Authored expected output: One listener handles all items and reads the id from the clicked item.
closest finds the nearest ancestor with data-id, so clicks on nested elements still resolve to the right item.
Common misconceptions
#- All events bubble.
- Delegation requires a listener per child.
- passive: true still allows preventDefault to work.
Follow-up practice
#- browser-internals-devtools-elements-network
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.