stopPropagation() in Browser Events

`stopPropagation()` stops the current event from continuing through the DOM's propagation path. It does not cancel the browser's default action and does not prevent other listeners on the same element from running.

When an event occurs, the browser can propagate it through ancestors during capture and bubbling. `event.stopPropagation()` tells the browser not to continue propagation to other targets along that path.

For example, if a button is inside a clickable container, clicking the button normally triggers the button's handler and can then bubble to the container. Calling `stopPropagation()` in the button handler prevents the event from reaching that container through propagation.

`stopPropagation()` is different from `preventDefault()`. The former controls event propagation; the latter asks the browser not to perform the event's default action, such as following a link or submitting a form. They can be used together when both behaviors need to be changed.

It is also different from `stopImmediatePropagation()`. `stopPropagation()` prevents the event from moving to other targets, while `stopImmediatePropagation()` additionally prevents later listeners on the same target from being invoked.

Key points

Interview questions

If two click listeners are attached to the same element, does `stopPropagation()` prevent the second one from running?

No. `stopPropagation()` prevents the event from continuing to other targets, but listeners already associated with the current target can still run. If the second listener must not execute, `stopImmediatePropagation()` is the relevant method. This distinction matters when several independent listeners are registered on one element.

What is the difference between `stopPropagation()` and `preventDefault()`?

`stopPropagation()` affects the event's journey through the DOM. `preventDefault()` asks the browser not to perform the event's default action. For example, a click handler can stop a click from reaching a parent while still allowing a link's default navigation. The two methods can also be called together when both effects are required.

What does `event.stopPropagation()` do?

It prevents an event from continuing its propagation to other DOM targets. For a bubbling event, this commonly means the event will not reach an ancestor listener. It does not cancel the browser's default action. It also does not stop other listeners attached to the same target.

How does `stopPropagation()` interact with event delegation?

Event delegation places a listener on an ancestor and relies on descendant events reaching that ancestor. If a descendant handler calls `stopPropagation()`, the delegated ancestor may never receive that event. Therefore, stopping propagation can unintentionally break delegated interactions. It should be used only when preventing that ancestor from observing the event is actually required.

Does `stopPropagation()` affect the capture phase?

Yes. Propagation includes the capture and bubbling phases, and stopping propagation prevents the event from continuing along its propagation path. The exact point at which it is called matters because listeners earlier in the path may already have executed. Understanding the event path, target phase, capture phase, and bubbling phase is therefore important when debugging propagation behavior.