event.preventDefault() cancels the browser's default action for an event, such as following a link or submitting a form. event.stopPropagation() stops the event from travelling further through the DOM to other elements, but it does not cancel the default action.
They solve different problems and are independent: you can call either one, both, or neither.
preventDefault() doesstopPropagation() does
stopPropagation()preventDefault()preventDefault() doesMany events have built-in browser behaviour: a click on <a href> navigates, a submit event on a <form> sends a request, a contextmenu event opens the menu, and a keydown of Space can scroll the page. Calling event.preventDefault() cancels that behaviour, provided the event is cancelable (check event.cancelable).
The event still bubbles to ancestors and other listeners on the same element still run. Only the default action is skipped. You can read event.defaultPrevented to see whether any handler has called it.
stopPropagation() doesDOM events travel in three phases: capturing (from window down to the target), the target phase, then bubbling (back up). event.stopPropagation() prevents the event from reaching listeners on other elements further along that path.
Clicking the button logs only the child message because the event never bubbles up to #parent. The click's default action, if any, still happens.
stopImmediatePropagation()stopPropagation() does not stop other listeners attached to the same element. event.stopImmediatePropagation() does: it prevents both propagation to other elements and any remaining listeners on the current element.
| Aspect | preventDefault() | stopPropagation() |
|---|---|---|
| Purpose | Cancel the browser's default action | Stop the event travelling through the DOM |
| Other listeners on the same element | Still run | Still run |
| Ancestors and descendants | Still receive the event | Do not receive it |
| Default action (navigation, submit) | Cancelled | Not affected |
| Requirement | Event must be cancelable | Works for any event that propagates |
| Check afterwards | event.defaultPrevented | No direct property (use event.cancelBubble only for legacy code) |
A common case is a clickable card that contains a link or button. You want the inner action to run without triggering the card's own handler and without navigating.
Returning false from a handler does not do either of these in addEventListener callbacks. It only cancels the default in legacy inline onclick="return false" handlers.
stopPropagation()Stopping propagation can break code that depends on events reaching document or window, such as analytics, dropdown "click outside to close" logic, and event delegation. Prefer these alternatives:
event.target or event.target.closest(".selector") in a delegated handler and ignore events you do not care about.event.target with event.currentTarget to run logic only for the exact element.{ once: true } or AbortController signals to remove listeners rather than blocking the event flow.preventDefault()For touchstart, touchmove and wheel events registered on window, document or body, browsers treat listeners as passive by default. In a passive listener, preventDefault() is ignored and the browser logs a warning. If you must cancel scrolling, register the listener on a specific element with { passive: false }.
Where possible, use CSS such as touch-action or overscroll-behavior instead of cancelling scroll in JavaScript.
stopPropagation() prevent the default action?No. A link still navigates and a form still submits unless a handler also calls preventDefault().
preventDefault() stop bubbling?No. The event continues to bubble to parent elements, and their listeners can check event.defaultPrevented.
stopPropagation() and stopImmediatePropagation()?stopPropagation() blocks other elements from receiving the event. stopImmediatePropagation() also blocks the remaining listeners on the current element.
preventDefault() not work in my handler?The event may not be cancelable, the listener may be passive, or the default action may already have happened. Check event.cancelable and the passive option.
Use preventDefault() to cancel what the browser would do by default, and stopPropagation() to keep an event from reaching other elements. They are independent, so combine them only when you need both. Prefer event delegation and target checks over stopping propagation, because blocking events can break other code that listens higher up the tree.