Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท DOM: Events

Event.stopPropagation() method

Reference for Event.stopPropagation() and stopImmediatePropagation(): stop an event from bubbling or capturing further, with examples and the pitfalls to avoid.

An event does not stop at the element where it happened. It travels through the DOM: down from window to the target (the capture phase), and then back up (the bubble phase). Calling event.stopPropagation() ends that journey. Listeners on the current element still finish, but no listener on any other element is called afterwards.

Its sibling stopImmediatePropagation() goes one step further: it also prevents the remaining listeners on the same element from running.

JavaScript
button.addEventListener("click", (event) => {
  event.stopPropagation();
});

Try it

Stopping a click from reaching the parent
stopPropagation vs stopImmediatePropagation

Syntax

JavaScript
event.stopPropagation()
event.stopImmediatePropagation()

Parameters

None.

Return value

undefined.

Usage notes

  • It does not cancel the default action. A link still navigates after stopPropagation(). Use preventDefault() for that. The two are independent and often confused.
  • It works in both directions. Called during the capture phase, it prevents the event from ever reaching the target. Called at the target or while bubbling, it prevents it from moving up.
  • Other listeners on the same element still run. Only stopImmediatePropagation() blocks them, and only the ones added after the current listener.
  • It breaks event delegation. If a child stops propagation, a delegated listener on document or a container never hears the event. Analytics and "click outside to close" menus are common casualties.
  • Prefer alternatives. Instead of stopping the event, check event.target or event.target.closest() in the parent, or let the event bubble and have the outer code ignore it when it does not apply.
  • cancelBubble. The legacy property event.cancelBubble = true does the same as stopPropagation(). Do not use it in new code.
  • Events that do not bubble. focus, blur, load and a few others do not bubble at all, so there is nothing to stop in the bubble phase (they still pass through the capture phase).

Browser support

stopPropagation() and stopImmediatePropagation() are supported in every browser.