preventDefault &
stopPropagation
Learn to intercept browser defaults and control event flow through the DOM: two essential tools for building interactive JavaScript applications.
What Are Browser Events?
Every time a user interacts with a webpage: clicking a link, submitting a form, pressing a key: the browser creates an Event object. This object carries information about what happened and travels through the DOM tree.
What Is a Default Action?
When certain events fire, the browser performs a built-in behavior automatically. These are called default actions. Sometimes you need to stop them.
href URLHow Events Propagate
When an event fires on an element, it doesn't just stay there. It travels through the DOM in three phases:
The preventDefault() Method
Calling event.preventDefault() tells the browser: "Do not perform your built-in action for this event." The event still propagates normally: only the default behavior is cancelled.
The stopPropagation() Method
Calling event.stopPropagation() tells the browser: "Do not pass this event to any parent elements." The default action still happens: only the propagation is halted.
Two Different Problems, Two Methods
Visualizing the Event Lifecycle
You can call preventDefault() at any phase to cancel the default action. Calling stopPropagation() at the target phase means the event never bubbles to parent listeners.
How to Apply Each Method
event.preventDefault() for defaults, event.stopPropagation() for flow control.Example 1: Form Validation
Problem: when a user submits a form, the browser reloads the page. We want to validate first and only submit if valid.
Example 2: Custom Navigation
Problem: you have anchor links that should trigger smooth scrolling or load content dynamically instead of navigating to a new page.
Example 3: Modal Close Button
Problem: clicking inside a modal should not close it, but clicking the overlay behind it should. Without stopPropagation, clicks bubble up to the overlay.
Example 4: Using Both Together
Sometimes you need to cancel the default action and prevent the event from bubbling. A common case: a delete button inside a clickable list item.
Bonus: stopImmediatePropagation()
stopPropagation() prevents the event from reaching parent elements. But if the same element has multiple listeners, they still all fire. To stop even those, use:
Common Mistakes
preventDefault() without event. in front: throws a ReferenceError.return false does nothing. It only works in inline HTML handlers (which you should avoid).Comparison Summary
| preventDefault() | stopPropagation() | |
|---|---|---|
| What it stops | Browser's default action | Event reaching parent elements |
| Event still bubbles? | Yes | No |
| Default action still happens? | No | Yes |
| Other same-element listeners fire? | Yes | Yes |
| Common use case | Form validation, custom links | Modals, nested click handlers |
| Can be used together? | Yes: they are independent | |
Question 1 of 5
A form automatically reloads the page on submit. Which method should you call in your event listener to prevent the page reload?
Question 2 of 5
You have a button inside a <div>. Both have click listeners. You want the button click to not trigger the div's listener. What do you call?
<div>, so its click listener never fires. preventDefault() only cancels browser defaults like navigation, not JavaScript listeners.Question 3 of 5
After calling event.preventDefault() on a link's click event, what happens?
Question 4 of 5
What is logged when the button is clicked?
Question 5 of 5
You want a delete button inside a clickable card to not navigate (the card is a link) and not trigger the card's click handler. What should you do?