Skip to article content
FrontendXLesson 10 / 20

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.

intermediate · about 35 minutes · 2 exercises

  • Predict
  • Implement

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

  1. Capture: from the window down to the target's parent.
  2. Target: at the target element.
  3. 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: true removes the listener after the first call.
  • passive: true promises not to call preventDefault, letting scroll run on the compositor.
  • signal lets an AbortController remove 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.

  1. Exercise 1Predict

    What does this log when the button is clicked?

    starter.javascript
    container.addEventListener('click', () => console.log('capture'), true);
    button.addEventListener('click', () => console.log('target'));
    container.addEventListener('click', () => console.log('bubble'));

    Reveal the reference answer
    reference.javascript
    capture, target, bubble

    Authored 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.

  2. Exercise 2Implement

    Implement click handling for a list of items using delegation and a data-id attribute.

    starter.javascript
    list.addEventListener('click', (event) => {
      // TODO
    });

    Reveal the reference answer
    reference.javascript
    list.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

Related resources

#

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.

© 2026 FrontendX. All rights reserved.