return false; in jQuery event handlers?
Published in
1 min readNov 14, 2013
More than often we want to cancel the default behaviour of browser. In jQuery, do we return false;
or e.preventDefault();
in event handlers?
Short Answer:
When in doubt, use e.preventDefault();
Longer Answer:e.preventDefault();
actually suffices most of the time. return false;
in jQuery event handler is doing more stuffs, basically the following:
e.preventDefault();
e.stopPropagation();
// And immediately stop the execution of the function (It “returns”!)
Event bubbling is normally what we would like to have. Having a e.stopPropagation();
without careful planning will usually lead to some unforeseen bugs that eat up precious development time.
Here’s a good read on this topic: jQuery Events: Stop (Mis)Using Return False