JavaScript Event Bubbling vs Capturing: Stop Propagation

Understand the DOM event capture and bubble phases, opt into capture listeners, and learn what stopPropagation() does and does not stop.

JavaScript Event Bubbling vs Capturing: Stop Propagation

Event Bubbling vs. Capturing — and How do you Stop Propagation?

When an event happens on a nested element, it can travel through the DOM tree. Understanding that route helps explain why a click handler on a parent can respond to a child click—and how to stop an event from reaching other elements.

For an event that participates in the full propagation path, the browser moves through three phases: capturing from outer ancestors toward the target, the target phase on the element where the event occurred, and bubbling back up through ancestors. Most event listeners use the bubbling phase by default. MDN: DOM events

See bubbling in action

Here is a button inside a card:

<div id="card">
  <button id="save">Save</button>
</div>

Add a click listener to each element:

const card = document.querySelector("#card");
const saveButton = document.querySelector("#save");

card.addEventListener("click", () => console.log("Card listener"));
saveButton.addEventListener("click", () => console.log("Button listener"));

When you click the button, its listener runs at the target, then the click bubbles to the card listener. Bubbling is what makes event delegation possible: a parent can handle events from its descendants.

Opt into capturing

To run a listener while the event travels down toward the target, set capture: true (or pass true as the third argument):

card.addEventListener("click", () => console.log("Card capture listener"), {
  capture: true,
});

saveButton.addEventListener("click", () => console.log("Button listener"));

For this nested click, the card's capture listener runs before the event reaches the button. Capture is useful when an ancestor needs to observe an event before descendant handlers. Most everyday click handling uses bubbling instead. MDN: addEventListener()

Stop an event from continuing

Call event.stopPropagation() inside a handler to stop the event from continuing along the capture or bubble path:

saveButton.addEventListener("click", (event) => {
  event.stopPropagation();
  console.log("Button handled; it will not bubble to the card");
});

This prevents the event from reaching later elements on its propagation path. It does not stop other listeners attached to the same element, and it does not cancel the browser's default action. MDN: stopPropagation()

If you also want to cancel a default action—such as following a link—use event.preventDefault() when the event is cancelable. These methods do different jobs:

link.addEventListener("click", (event) => {
  event.preventDefault(); // Stop the link's default navigation
  event.stopPropagation(); // Stop this event traveling to ancestors
});

If you need to stop additional listeners on the same element as well as propagation, event.stopImmediatePropagation() does that. Use it carefully because it can interfere with other code listening on that element.

When should you stop propagation?

Use stopPropagation() when a specific interaction should not trigger handlers higher in the DOM—for example, clicking inside a popover should not activate a parent click-to-close handler. Avoid adding it everywhere: stopping propagation can break event delegation or other components that rely on bubbling.

Not every event bubbles. Check the event's behavior before relying on delegation or propagation. For example, focus does not bubble, while focusin does. MDN: Event.bubbles

Try it yourself

Add capture and bubble listeners to a parent <div> and its child button. Click the button and note the order. Then call stopPropagation() in the button's handler and observe which parent listener no longer runs. Try preventDefault() separately on a link to compare the effects.

Recap

  • Capturing travels from outer ancestors toward the event target.
  • Bubbling travels from the target back up through ancestors; it is the default listener phase.
  • { capture: true } registers a listener for the capture phase.
  • stopPropagation() stops travel to other elements, but not same-element listeners or default browser behavior.
  • Use preventDefault() to cancel a default action and stopImmediatePropagation() to stop other listeners on the same element.

What's next?

Next, we’ll learn what code splitting is and how it can help load an application more efficiently.

Written by

Dev Likhon

Dev Likhon

Full Stack Developer & Tech Educator

6+ years of professional development experience.

About Me
Share:

Comments

Share what you learned or ask a question.

Loading comments...