Skip to content
Tutorials Center
My learning

Topics

Lesson 88 of 156 ยท The DOM

Bubbling, Capturing and Delegation

Learn how events travel through the DOM in capture, target and bubble phases, when to stop propagation, and how event delegation works.

When you click a button inside a list item inside a section, which element did you click? In a sense, all of them. The browser doesn't deliver the event to the button alone: it sends it on a journey down through the button's ancestors and back up again. Every element along the way gets a chance to react.

This journey is called event propagation, and understanding it explains several puzzles: why a click on a child triggers a listener on its parent, why event.target and event.currentTarget can differ, and why one listener on a container can handle clicks on hundreds of buttons. That last technique, event delegation, is one of the most useful patterns in DOM programming. The HTML course gave it a brief preview in Event Attributes and Event Listeners; here you will learn how it really works.

The three phases

When an event is dispatched on an element (the target), it travels in three phases:

  1. Capture phase. The event starts at window and travels down through document, <html>, <body> and every ancestor, towards the target.
  2. Target phase. The event arrives at the target element itself.
  3. Bubble phase. The event travels back up through the same ancestors to window.
TEXT
window โ”€โ”€captureโ”€โ”€โ–ถ document โ”€โ–ถ html โ”€โ–ถ body โ”€โ–ถ ul โ”€โ–ถ li โ”€โ–ถ [button]
window โ—€โ”€โ”€bubbleโ”€โ”€โ”€ document โ—€โ”€ html โ—€โ”€ body โ—€โ”€ ul โ—€โ”€ li โ—€โ”€ [button]

By default, addEventListener() listens in the bubble phase, which is why you usually don't think about capture at all. Pass { capture: true } (or just true as the third argument) to listen in the capture phase instead. Listeners on the target itself run in the target phase, in the order they were added.

The best way to make this concrete is to watch it:

Watching the phases

Click the button and you see capture listeners from the outside in, then the target, then bubble listeners from the inside out. Now click the padding of the inner paragraph: the target changes to inner and the journey is shorter. event.eventPhase is a number (1, 2 or 3) telling you which phase a listener is running in.

target vs currentTarget

Propagation is why the event object has two element properties:

  • event.target โ€” where the event started. It stays the same for the whole journey.
  • event.currentTarget โ€” the element whose listener is running right now. It changes at every step.

In the example above, a listener on outer sees target as the button but currentTarget as outer. Note that target is the deepest element under the pointer. If your button contains an icon in a <span> or an <svg>, a click on the icon makes the icon the target. You will handle that with closest() below.

Stopping propagation

Three methods change what happens to an event, and they are often confused:

  • event.stopPropagation() โ€” the event stops travelling. Other listeners on the current element still run, but no further ancestors (or descendants, in the capture phase) receive it.
  • event.stopImmediatePropagation() โ€” stops travelling and prevents any remaining listeners on the current element from running.
  • event.preventDefault() โ€” has nothing to do with propagation. It cancels the browser's default action (following a link, submitting a form, toggling a checkbox), while the event keeps travelling normally.
stopPropagation vs preventDefault

Click the card, then the button, then the link. The button stops the event, so the card never hears about it. The link only prevents navigation; the event keeps bubbling and the card still opens.

Use stopPropagation() sparingly. Code elsewhere on the page may depend on events bubbling: analytics, a "click outside to close" menu, a delegated listener. Stopping propagation silently breaks all of them. Often a better fix is for the parent listener to check where the click came from, for example if (event.target.closest("button")) return;.

Events that don't bubble

Most events bubble, but not all. The most important exceptions:

  • focus and blur โ€” use focusin and focusout, which do bubble.
  • mouseenter/mouseleave and pointerenter/pointerleave โ€” use mouseover/mouseout or pointerover/pointerout for bubbling versions.
  • load, error and scroll on individual elements, and media events like play.

If you need to catch a non-bubbling event from a container, either use its bubbling sibling or listen in the capture phase, which every event goes through: form.addEventListener("blur", handler, true). You can check event.bubbles to see whether any event bubbles.

Event delegation

Because events bubble, you don't need a listener on every element. Put one listener on a common ancestor and inspect event.target to find out which element was involved. That is event delegation, and it has real advantages:

  • Fewer listeners. A table with 500 rows needs one listener, not 500.
  • Dynamic content just works. Elements added later are covered automatically, with no re-binding after every render.
  • Simpler cleanup. Removing elements never leaves orphaned listeners behind.

The reliable recipe has three steps:

  1. Listen on a stable container that exists for the lifetime of the content.
  2. Use event.target.closest(selector) to find the element you care about. It works even when the click lands on an icon or text inside it.
  3. Check that the found element is inside your container (with container.contains()), so you don't react to a matching ancestor outside it.

A clean way to scale this to many actions is to describe each action in the HTML with a data-action attribute and dispatch on it:

Delegated actions with data-action

Add items and click their buttons. Every button works, even though none of them has its own listener. Click the bin icon itself: the target is the <span>, yet closest() still finds the button. (The fixed markup in the template string is our own; the product name is inserted with textContent, as you learned in Changing Content.)

Delegating other events

Delegation works for any bubbling event, not just clicks. Form containers are a great example: one input listener on a <form> hears every field. For focus, remember to use focusin/focusout:

Delegating focus and input

When not to delegate

Delegation is a default, not a rule. A single button with its own logic can simply have its own listener. High-frequency events such as pointermove on a big container run your code for every movement over every child, so attach those directly to the element that needs them. And pointerenter/pointerleave don't bubble at all, so they can't be delegated without the capture trick.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In what order do the phases of an event happen?

    The event travels down from window (capture), reaches the target, then travels back up (bubble). Listeners default to the bubble phase.
  2. 2. A listener on a <ul> handles a click on a <button> inside an <li>. What are event.target and event.currentTarget?

    target is where the event started; currentTarget is the element whose listener is running.
  3. 3. Which call cancels a link's navigation but lets the click keep bubbling to ancestors?

    preventDefault() only cancels the default action. Propagation is a separate thing, controlled by the stop methods.
  4. 4. You want one listener on a form to know when any field gains focus. Which event should you listen for?

    focus doesn't bubble, so the form never hears it in the bubble phase. focusin is the bubbling version.
  5. 5. Why use event.target.closest(".delete") instead of checking event.target.matches(".delete") in a delegated listener?

    target is the deepest element clicked. closest() checks the element and its ancestors, so it finds the button either way.

Summary

  • Events travel in three phases: capture (down from window), target, and bubble (back up). Listeners use the bubble phase unless you pass { capture: true }.
  • event.target is where the event started; event.currentTarget is the element whose listener is running.
  • stopPropagation() stops the journey, stopImmediatePropagation() also skips remaining listeners on the element, and preventDefault() only cancels the default action.
  • focus, blur, and *enter/*leave don't bubble; use focusin/focusout, the over/out events or capture listeners.
  • Event delegation: one listener on a container plus closest() handles any number of children, including ones added later; data-action attributes keep it organized.

Next: Working with Forms โ€” reading values, FormData, validation and submit handling.