Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

event.preventDefault() vs. return false

When I want to prevent other event handlers from executing after a certain event is fired, I can use one of two techniques. I'll use jQuery in the examples, but this applies to plain-JS as well:

1. event.preventDefault()

$('a').click(function (e) {
 // custom handling here
 e.preventDefault();
});

2. return false

$('a').click(function () {
 // custom handling here
 return false;
});

Is there any significant difference between those two methods of stopping event propagation?

For me, return false; is simpler, shorter and probably less error prone than executing a method. With the method, you have to remember about correct casing, parenthesis, etc.

Also, I have to define the first parameter in callback to be able to call the method. Perhaps, there are some reasons why I should avoid doing it like this and use preventDefault instead? What's the better way?

Answer*

Draft saved
Draft discarded
Cancel
10
  • 6
    @rds That says "preventDefault doesn't stop further propagation of the event through the DOM. event.stopPropagation should be used for that." Commented Jul 24, 2011 at 23:40
  • An answer on a closely-related question alleges that prior to HTML 5, returning false from an event handler wasn't specced as doing anything at all. Now, maybe that's an incorrect interpretation of the (hard to understand) spec, or maybe despite it not being specced literally all the browsers interpreted return false the same as event.preventDefault(). But I dunno; it's enough to make me take this with a pinch of salt. Commented Jan 24, 2016 at 0:13
  • 11
    I hate how every javascript search result in google is actually about jQuery, +1 Commented Oct 11, 2016 at 21:26
  • He has tagged it as both JavaScript and jQuery, and both are correct. jQuery is merely a sub-set of JavaScript, not its own language. Commented Jan 28, 2020 at 5:26
  • 1
    MSDN is now rewriting history to pretend that its history is now that of current Firefox. Rewriting history is nothing new for governments, and is a tool of power, for perception management. How casually and laughably MSDN rewrites history with event.returnValue. That's not reality, however. lists.w3.org/Archives/Public/www-style/2010Sep/0458.html Commented Nov 14, 2022 at 16:14

lang-js

AltStyle によって変換されたページ (->オリジナル) /