designwithfocus

JavaScript & front end

What Is Event Bubbling? How Clicks Climb the DOM

By Design With Focus Team 7 min read
Simple sketch of a click moving from a button up through nested boxes

Short answer

Event bubbling means a click on a child also reaches its ancestor elements, from the inside outward. MDN shows a button, then its div, then the body. The target stays the element that was hit. currentTarget is the element whose listener is running. stopPropagation halts the climb. stopImmediatePropagation also skips later listeners on the same element.

Last verified against the sources listed below.

Key takeaways

  • Bubbling runs from the target's parent back up toward the window, if bubbles is true.
  • Capture runs the other way, from the window down, and it is off unless you ask.
  • event.target stays the original element. event.currentTarget is the listener's element.
  • stopPropagation does not cancel the browser's default action, and it does not skip sibling listeners.
  • React's docs say events bubble up the tree, except onScroll, and stopPropagation blocks parents.

What is event bubbling is the upward half of a DOM click. The event starts at the element you hit, then parent elements see it too. MDN’s lesson shows a button inside a div inside the body.

One click runs the button listener, then the div, then the body. That climb is the bubble.

JavaScript is the language you register those listeners in. The behavior itself is a browser event, which MDN notes is a Web API rather than a core language feature.

An SVG to JSX converter turns a drawing into a React component. Clicks on that component still follow the bubble rules below.

What is event bubbling in JavaScript?

In JavaScript you see bubbling when a parent listener runs for a child click. MDN’s example registers the same handler on the button, the container, and document.body. The log is BUTTON, then DIV, then BODY.

The click “bubbles up” from the innermost element.

The useful pattern is delegation. Put one listener on a parent of many tiles. When a tile is clicked, the event reaches the parent, and event.target is the tile.

You avoid a listener on every child. MDN uses that pattern to recolor a grid of tiles from a single container listener.

target and currentTarget split the two elements. target stays the original element for the whole climb. currentTarget is the element whose listener is running right now.

If you paint currentTarget, you paint the container. If you paint target, you paint the tile.

How does event bubbling work?

The event walks the tree in phases, and bubbling is the last one. MDN’s eventPhase page lists the values. CAPTURING_PHASE is 1.

The event moves from the Window through the document and the root element until it reaches the target’s parent. Listeners registered for capture run here.

AT_TARGET is 2, when the event is on the target. BUBBLING_PHASE is 3. The event walks back through ancestors in reverse, from the parent toward the Window.

That phase runs only if bubbles is true. If bubbles is false, processing ends after the target phase. NONE (0) means the event is not being processed.

UI Events describes this flow through a tree. It also shows that not every event type uses every phase. A load listener attached only for the bubbling phase would not run.

focus and blur do not bubble. focusin and focusout do. Check bubbles before you depend on a parent listener.

Phase Direction How to listen
Capturing (1) From the window down to the target’s parent addEventListener with capture: true, or true as the third argument
Target (2) At the element the event was fired on A listener on that element
Bubbling (3) From the parent back up toward the window, only if bubbles is true A normal listener. MDN says this is the default for almost all handlers

MDN’s lesson says capture is off unless you pass the capture option. In the sample, body and div use { capture: true }, and the log flips to BODY, then DIV, then BUTTON. The W3C history note on that page is that the two phases both survived so browsers could agree.

How do you stop event bubbling?

Call stopPropagation() inside the handler that should be the last one. MDN’s stopPropagation page says the method prevents further propagation in the capturing and bubbling phases. The lesson uses it so a click on a video plays the video and does not also reach the box that would hide it.

It does not do two other jobs. It does not cancel default behavior. A link can still navigate.

Use preventDefault() for that. It also does not block other listeners on the same element. stopImmediatePropagation does.

If several listeners were added for the same type, they run in registration order. After stopImmediatePropagation(), no remaining listener runs, on that element or any other.

The DOM Standard defines the methods as flags. stopPropagation() sets the stop propagation flag. stopImmediatePropagation() sets that flag and the stop immediate propagation flag.

cancelBubble is marked as a legacy alias of stopPropagation(). Prefer the method name you can search for.

What is event bubbling in HTML?

In HTML, bubbling follows the nesting you wrote. MDN’s markup is a button inside a div inside body. The button is the target.

The div and the body are ancestors. A click listener on the div runs when the button is clicked, because the button is inside the div. Clicking empty space on the div does not run the button’s listener.

That is also why a control inside a card can fire the card. If the card hides itself on click, and the button inside should do something else, the button’s handler needs stopPropagation(). Otherwise both handlers run.

The HTML structure causes the climb. The JavaScript only chooses whether to keep it.

Delegation belongs in HTML for the same reason. A list of buttons does not need a listener copied onto each new row. The parent list hears the click.

Read event.target to see which button it was. If you replace the rows, the parent listener is still there.

What is event bubbling in React?

React’s current docs describe the same upward order on the component tree. The event guide says a handler also catches events from children. The event starts where it happened and goes up the tree.

In the toolbar sample, the button’s onClick runs first, then the parent div’s onClick. A click on the toolbar itself runs only the div.

All events propagate in React except onScroll, which only works on the tag you attach it to. To stop the climb, call e.stopPropagation() on the event argument. The docs then show the button alert without the toolbar alert.

They also say stopPropagation and preventDefault are unrelated. One blocks parent handlers. The other blocks the browser default, such as a form submit that would reload the page.

React names three phases on that page. The event travels down and calls onClickCapture handlers. It runs the clicked element’s onClick.

It travels up and calls onClick handlers. Capture is the tool the docs suggest for logging or routers, when you need the event even if a child called stopPropagation(). The page does not say to hang listeners on document.

Follow the capture prop and the tree it describes.

An Angular template is still HTML elements in the DOM. This guide does not add an Angular-specific phase. Use the table above for the browser, and read Angular’s current event docs if a wrapper claims to change it.

The SVG component you generate will bubble the same way as any other element in the tree, until a handler stops the event. ---

Frequently asked questions

How do you stop event bubbling?

Call stopPropagation on the event. MDN says it stops further propagation in the capture phase and the bubble phase. It does not stop the browser's default action, such as following a link, and it does not stop other listeners already registered on the same element. Use preventDefault when you want to cancel the default action. Use stopImmediatePropagation when you also need to skip the remaining listeners on that element. The DOM spec says cancelBubble is a legacy alias of stopPropagation.

What is the difference between target and currentTarget?

MDN's bubbling lesson says target is the element the event was fired on, the innermost one. currentTarget is the element the listener is attached to. While the event climbs, target stays the button you clicked. currentTarget changes to the div, then the body, as each listener runs. Event delegation uses target so one parent listener can see which child was actually hit. Use currentTarget when you need the element that owns the listener.

Do all events bubble?

No. MDN's eventPhase page says the bubbling phase runs only if bubbles is true. If bubbles is false, the event finishes after the target phase. UI Events says focus and blur do not bubble, while focusin and focusout do. The same spec says a load listener attached only for the bubbling phase would not run. React's event guide adds one framework exception: all events propagate in React except onScroll, which only runs on the element you attach it to.

What is event bubbling in React?

React's current event guide says an event bubbles or propagates up the tree. It starts where the event happened, then goes up. A button's onClick runs, then a parent div's onClick. Call stopPropagation on the event argument to keep parent components from handling it. The same page says preventDefault is a different call and does not stop propagation. The guide describes onClickCapture for the downward phase. It does not describe attaching listeners to the document.

Does Angular replace event bubbling?

This guide does not cite an Angular listener API. Event bubbling in the browser is the DOM behavior MDN and UI Events describe: from the target, through ancestors, when bubbles is true. A page built with Angular still uses those elements. If a click listener sits on a parent element, the child's click can reach it the same way a plain HTML button reaches its div. Check the framework's current docs before you assume a wrapper changed the phase.

Sources