preventDefault vs stopPropagation
They solve different problems.
event.preventDefault()cancels the browser’s built-in action for the event (following a link, submitting a form, checking a box). The event still travels through the DOM.event.stopPropagation()stops the event from reaching other elements (ancestors while bubbling, descendants while capturing). The browser’s default action still happens.
<div id="card">
<a id="link" href="/pricing">Pricing</a>
</div>
card.addEventListener('click', () => console.log('card clicked'));
link.addEventListener('click', (e) => {
e.preventDefault(); // no navigation; "card clicked" still logs
});
// versus
link.addEventListener('click', (e) => {
e.stopPropagation(); // navigation happens; "card clicked" does not log
});
1. preventDefault: common uses
form.addEventListener('submit', (e) => {
e.preventDefault(); // stop the full-page submit
save(new FormData(form));
});
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') e.preventDefault(); // don't submit on Enter
});
It only works on cancelable events. Check event.cancelable before, and event.defaultPrevented after.
2. stopPropagation: common uses
// A delete button inside a clickable row
deleteBtn.addEventListener('click', (e) => {
e.stopPropagation(); // don't also open the row
remove(row);
});
Use it sparingly. Stopping propagation hides the event from everything above, including analytics, “click outside to close” handlers and delegated listeners. A less invasive option is to let the event bubble and have the parent check what was clicked:
row.addEventListener('click', (e) => {
if (e.target.closest('button')) return; // a button handled it
open(row);
});
3. stopImmediatePropagation
stopPropagation still lets other listeners on the same element run. stopImmediatePropagation stops those too.
btn.addEventListener('click', (e) => e.stopImmediatePropagation());
btn.addEventListener('click', () => console.log('never runs'));
4. Passive listeners can’t prevent default
window.addEventListener('touchmove', (e) => e.preventDefault(), { passive: true });
// Ignored, with a console warning
passive: true promises the browser you won’t call preventDefault, so it can scroll without waiting for your handler. Major browsers treat touchstart, touchmove and wheel listeners on the window, document and body as passive by default. Pass { passive: false } if you really need to block scrolling.
5. return false
- In an inline handler (
onclick="return false") or a handler assigned as a property (el.onclick = ...), it prevents the default action. - In
addEventListener, it does nothing. - In jQuery, it does both
preventDefaultandstopPropagation.
6. In React
React’s event object supports both methods. stopPropagation stops other React handlers further up the tree. Since React 17, React attaches its listeners to the root container, not document, so mixing React handlers with native document listeners behaves more predictably than it used to.
Summary
| Default action | Other elements | Other listeners on this element | |
|---|---|---|---|
preventDefault() |
Cancelled | Still notified | Still run |
stopPropagation() |
Still happens | Not notified | Still run |
stopImmediatePropagation() |
Still happens | Not notified | Don’t run |
| Both | Cancelled | Not notified | Still run |
Follow-up questions to expect
- “A modal should close when you click the backdrop but not its content. How?” Compare
event.targetwithevent.currentTargeton the backdrop, so you don’t needstopPropagationat all. - “Does
preventDefaultstop bubbling?” No. - “What are the phases of an event?” Capture, target, bubble. See event propagation and event delegation.
