DOM events: stopPropagation vs preventDefault() vs. return false
Which one to use between `event.stopPropagation()`, `event.preventDefault()` and `return false` in events
I feel like I’m always confused by one thing when it comes to handling DOM events in JavaScript.
When should I call stopPropagation() on the event object?
When should I call preventDefault() on the event object?
Should I return false?
Event.stopPropagation
Suppose I want to handle a click event on an element.
By default an event on a DOM element is fired on the specific element clicked (say, a button) and will be propagated to all its parent elements tree, unless it’s stopped.
I want to make sure the event is handled in my event handler, and it will “stop” there.
You can stop the propagation by calling stopPropagation() method of an Event object, usually at the end of the event handler:
const link = document.getElementById('my-link')
link.addEventListener('mousedown', event => {
// process the event
// ...
event.stopPropagation()
})
Event.preventDefault
Calling the preventDefault() method of the event object will cancel the default handling that the browser is programmed to execute.
Opening a new page on an a element click event, for example.
Or submitting a form on the submit event.
Calling preventDefault() is what you need to do to completely customize the action. Perhaps by creating a fetch request to load some JSON instead of opening a new page on a link click.
Other event handlers defined on this same element will execute. Unless you call event.stopImmediatePropagation().
Returning false
This is especially confusing to former (or current) jQuery developers. In jQuery, returning false from an event handler automatically called Event.preventDefault and Event.stopPropagation for us.
In vanilla JavaScript, return false in an event handler does nothing.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.