What's the difference between e.preventDefault(); and return false? [duplicate]
Asked Answered
I

1

43
$("a.avatar").click(function(e){
      e.preventDefault();
      $("#thumbnails").fadeIn();
    });

and

$("a.avatar").click(function(e){
      $("#thumbnails").fadeIn();
          return false;
    });

Both can achieve the same goal for me.

Inexpressive answered 7/1, 2010 at 2:7 Comment(0)
W
49

Returning false from jQuery event handlers is equivalent to calling both, e.preventDefault and e.stopPropagation.

So the difference is that preventDefault will only prevent the default event action to occur, i.e. a page redirect on a link click, a form submission, etc. and return false will also stop the event flow.

Wien answered 7/1, 2010 at 2:9 Comment(2)
can you give an example where stopping event flow is not desirable?Pierce
css-tricks.com/return-false-and-prevent-default check a demo.Gomorrah

© 2022 - 2024 — McMap. All rights reserved.