40

Is there any use to return values from .click() and .change() handlers (like return true or return false)?

jwueller
  • 30,582
  • 4
  • 66
  • 70
Buzzzz
  • 887
  • 3
  • 11
  • 18

2 Answers2

51

return false; will stop the event from bubbling up AND suppress the default action.

In otherwords, returning false is analogous to these two lines

event.stopPropagation();
event.preventDefault();

For events that bubble and are cancelable, at least.

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
23

return false in such a handler results in event.stopPropagation() (only for jQuery event handlers, as Tim Down suggested in the comments) and event.preventDefault() being called automatically by jQuery.

return true is the same as returning nothing (no action is taken by jQuery).

jwueller
  • 30,582
  • 4
  • 66
  • 70
  • 3
    In that case, jQuery is taking liberties here: for non-jQuery event handlers, `return false` only prevents the default action and does not stop the event from bubbling. – Tim Down Dec 07 '10 at 17:19
  • @Tim Down: Good to know. I'll add that to my answer. – jwueller Dec 07 '10 at 17:20
  • @TimDown To clarify, by "non-jQuery event handlers", do you mean handlers that were attached by other means than the jQuery methods (.on or .live/.bind/.delegate), like addEventListener? – Jo Liss Nov 28 '11 at 14:29
  • 5
    @JoLiss: `return false` has no effect in event listeners attached using DOM's `addEventListener()`. It works to suppress default behaviour in so-called DOM0 event handlers, such as `document.body.onclick = function() { return false; };` – Tim Down Nov 28 '11 at 14:54
  • By default, a plain `return` (with no specified return value) or just "falling off the edge of the function"-returning will cause the function to return `undefined`. How does jQuery react to this? – zrajm Jul 15 '19 at 17:31