Basics

preventDefault vs stopPropagation — what's the difference and which to use

Illustration: one hand catches the action falling out of a button, another blocks the way up the stairs

Open almost any event handler AI wrote, and you'll see this pair:

e.preventDefault()
e.stopPropagation()

"Just in case." The form stopped reloading the page — seems to work. A week later you notice something odd: the dropdown doesn't close when you click outside it. Analytics stopped counting clicks on that button.

Here's what's going on. These two calls do completely different things. One cancels what the browser would have done. The other keeps the event from reaching parent elements. Adding the second one "to be safe" is like cutting power to the whole house instead of turning off the alarm clock.

In short: what each one does

preventDefault — "browser, don't do your default action". The event keeps travelling as usual.

stopPropagation — "don't pass this event any further up the page". The browser still does its default action.

To see the difference, remember how an event moves through the page. You clicked a button inside a card inside a list. The button gets the event first, then it bubbles up: the card, the list, all the way to document. That's a path through the DOM tree.

Side by side

  • What it cancels. preventDefault cancels the browser's action: submitting a form with a reload, following a link, ticking a checkbox, the context menu, scrolling. stopPropagation cancels nothing — it only cuts off bubbling.
  • Who it affects. preventDefault affects only the browser. All your handlers higher up still fire. stopPropagation is the opposite: the browser does its thing, and parent handlers stay silent.
  • Example. A link with stopPropagation still opens the new page. A link with preventDefault goes nowhere, but every parent sees the click.
  • Typical correct use. preventDefault — in a form's submit handler, to send the data via fetch without a reload. stopPropagation — when a "delete" button inside a card shouldn't also open the card itself.
  • The cost of an extra call. An extra preventDefault breaks behavior users expect: a link won't open in a new tab, a checkbox won't tick. An extra stopPropagation breaks someone else's code: menus that close "on outside click", analytics trackers, anything listening for clicks on document.
  • How to tell it worked. For preventDefault there's the e.defaultPrevented flag — handlers higher up can see it. stopPropagation has no such flag: the event simply never arrives, and figuring out why is harder.

Hidden traps

preventDefault silently fails on scroll. Browsers make touchstart, touchmove and wheel listeners on the window and document "passive" by default. In those, preventDefault is ignored and the console shows a warning: "Unable to preventDefault inside passive event listener". If you really need to cancel scrolling, pass { passive: false } as the third argument to addEventListener.

In React, return false cancels nothing. In old HTML, onclick="return false" cancelled the browser action. In React, a handler that returns false just returns false. You need an explicit e.preventDefault().

There's a third sibling — stopImmediatePropagation. It not only stops bubbling but also prevents other handlers on the same element from running. Rarely needed, and almost always a sign that your handlers are tangled up with each other.

Which one is for you

  • A form sent via fetch, no page flash — preventDefault only. That's how every contact form does it.
  • A custom button instead of a link, custom drag-and-drop, custom context menu — preventDefault.
  • A button inside a clickable card — stopPropagation on the button. But it's better to check in the card's handler where the click came from: if (e.target.closest("button")) return. That way you don't cut anyone off.
  • "So it definitely works" — neither. First figure out which behavior is actually in the way.

My verdict: preventDefault is a normal working tool. stopPropagation is a last resort. Every time you see it in a callback, ask: who exactly is it meant to silence, and will it silence someone else's code too?

How to ask AI not to add both

Weak promptthe form reloads the page, fix it
Strong promptThe form reloads the page on submit. I send the data via fetch. Add only e.preventDefault() to the submit handler. Do not add stopPropagation: the page has a menu that closes on document clicks, plus click analytics. If there is already a stray stopPropagation somewhere in the file, find it and explain why it is there.

Can I call both?

You can, and sometimes it's right. For example, a custom context menu inside another clickable block. But it should be a deliberate decision, not a reflex.

Does preventDefault undo something that already happened?

No. It only works while the event is being handled. Once the new page is loading, it can't cancel the navigation.

Why did my menu stop closing after stopPropagation?

Because the menu listens for clicks on document to detect "clicked outside". You cut off bubbling — the click never reaches document anymore.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →