84

Firstly, in JavaScript's event model, you will come across a concept called as event bubbling (which makes an event to propagate from child element to a parent element). In order to avoid such kind of bubbling effect, many developers use an event method called stopPropagation( ). Alternatively, developers have started to use return false statement to stop such propagation. Now, there is another terminology called preventDefault( ). As the name indicates, this method prevents any default behavior of an element to trigger. Best use case is to prevent an anchor tag to open a link.

You may come across a scenario where you would like to prevent the anchor tag from opening a link (default behavior) as well as stop the event from going up to the parent. In such situation, instead of writing two lines of code, you can get it done in single line i.e; return false

Manashvi Birla
  • 2,837
  • 3
  • 14
  • 28
Mike
  • 841
  • 1
  • 7
  • 5
  • 2
    This is a great topic to cover, but needs to be in a question and answer format. Also, there's already a good question with good answers on this topic, which I've marked here. – jdphenix May 27 '15 at 05:11
  • This will be helpful too : http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false – Viraj Nalawade May 27 '15 at 05:13
  • The content in this question seems to be coming from a company's blog. This post is probably well intended, but it is not in a question-answer format, and it doesn't cite the source either. –  May 27 '15 at 05:22
  • 18
    It's not a duplicate, it address the "return false" instruction specifically which is not the case in the top link dup question. – Jean-Philippe Martin Jan 14 '16 at 16:14
  • 4
    I hope there is an option to downvote those moderators. – towry Apr 02 '18 at 02:14
  • 2
    @Jean-PhilippeMartin FWIW there are answers under the target that also cover return false quite comprehensively. – TylerH Aug 15 '19 at 14:17

1 Answers1

184

return false;


return false; does 3 separate things when you call it:

  1. event.preventDefault() – It stops the browsers default behaviour.
  2. event.stopPropagation() – It prevents the event from propagating (or “bubbling up”) the DOM.
  3. Stops callback execution and returns immediately when called.

Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably, return false does not stop the event from bubbling up.

preventDefault();


preventDefault(); does one thing: It stops the browsers default behaviour.

When to use them?


We know what they do but when to use them? Simply it depends on what you want to accomplish. Use preventDefault(); if you want to “just” prevent the default browser behaviour. Use return false; when you want to prevent the default browser behaviour and prevent the event from propagating the DOM. In most situations where you would use return false; what you really want is preventDefault().

Examples:


Let’s try to understand with examples:

We will see pure JAVASCRIPT example

Example 1:

<div onclick='executeParent()'>
  <a href='http://stackoverflow.com' onclick='executeChild()'>Click here to visit stackoverflow.com</a>
</div>
<script>
  function executeChild() {
    alert('Link Clicked');
  }

  function executeParent() {
    alert('div Clicked');
  }
</script>

Run the above code you will see the hyperlink ‘Click here to visit stackoverflow.com‘ now if you click on that link first you will get the javascript alert Link Clicked Next you will get the javascript alert div Clicked and immediately you will be redirected to stackoverflow.com.

Example 2:

<div onclick='executeParent()'>
  <a href='http://stackoverflow.com' onclick='executeChild()'>Click here to visit stackoverflow.com</a>
</div>
<script>
  function executeChild() {
    event.preventDefault();
    event.currentTarget.innerHTML = 'Click event prevented'
    alert('Link Clicked');
  }

  function executeParent() {
    alert('div Clicked');
  }
</script>

Run the above code you will see the hyperlink ‘Click here to visit stackoverflow.com‘ now if you click on that link first you will get the javascript alert Link Clicked Next you will get the javascript alert div Clicked Next you will see the hyperlink ‘Click here to visit stackoverflow.com‘ replaced by the text ‘Click event prevented‘ and you will not be redirected to stackoverflow.com. This is due > to event.preventDefault() method we used to prevent the default click action to be triggered.

Example 3:

<div onclick='executeParent()'>
  <a href='http://stackoverflow.com' onclick='executeChild()'>Click here to visit stackoverflow.com</a>
</div>
<script>
  function executeChild() {
    event.stopPropagation();
    event.currentTarget.innerHTML = 'Click event is going to be executed'
    alert('Link Clicked');
  }

  function executeParent() {
    alert('div Clicked');
  }
</script>

This time if you click on Link the function executeParent() will not be called and you will not get the javascript alert div Clicked this time. This is due to us having prevented the propagation to the parent div using event.stopPropagation() method. Next you will see the hyperlink ‘Click here to visit stackoverflow.com‘ replaced by the text ‘Click event is going to be executed‘ and immediately you will be redirected to stackoverflow.com. This is because we haven’t prevented the default click action from triggering this time using event.preventDefault() method.

Example 4:

<div onclick='executeParent()'>
  <a href='http://stackoverflow.com' onclick='executeChild()'>Click here to visit stackoverflow.com</a>
</div>
<script>
  function executeChild() {
    event.preventDefault();
    event.stopPropagation();
    event.currentTarget.innerHTML = 'Click event prevented'
    alert('Link Clicked');
  }

  function executeParent() {
    alert('Div Clicked');
  }
</script>

If you click on the Link, the function executeParent() will not be called and you will not get the javascript alert. This is due to us having prevented the propagation to the parent div using event.stopPropagation() method. Next you will see the hyperlink ‘Click here to visit stackoverflow.com‘ replaced by the text ‘Click event prevented‘ and you will not be redirected to stackoverflow.com. This is because we have prevented the default click action from triggering this time using event.preventDefault() method.

Example 5:

For return false I have three examples and all appear to be doing the exact same thing (just returning false), but in reality the results are quite different. Here's what actually happens in each of the above.

cases:

  1. Returning false from an inline event handler prevents the browser from navigating to the link address, but it doesn't stop the event from propagating through the DOM.
  2. Returning false from a jQuery event handler prevents the browser from navigating to the link address and it stops the event from propagating through the DOM.
  3. Returning false from a regular DOM event handler does absolutely nothing.

Will see all three example.

  1. Inline return false.

<div onclick='executeParent()'>
  <a href='http://stackoverflow.com' onclick='return false'>Click here to visit stackoverflow.com</a>
</div>
<script>
  var link = document.querySelector('a');

  link.addEventListener('click', function() {
    event.currentTarget.innerHTML = 'Click event prevented using inline html'
    alert('Link Clicked');
  });


  function executeParent() {
    alert('Div Clicked');
  }
</script>
  1. Returning false from a jQuery event handler.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <a href='http://stackoverflow.com'>Click here to visit stackoverflow.com</a>
</div>
<script>
  $('a').click(function(event) {
    alert('Link Clicked');
    $('a').text('Click event prevented using return FALSE');
    $('a').contents().unwrap();
    return false;
  });
  $('div').click(function(event) {
    alert('Div clicked');
  });
</script>
  1. Returning false from a regular DOM event handler.

<div onclick='executeParent()'>
  <a href='http://stackoverflow.com' onclick='executeChild()'>Click here to visit stackoverflow.com</a>
</div>
<script>
  function executeChild() {
    event.currentTarget.innerHTML = 'Click event prevented'
    alert('Link Clicked');
    return false
  }

  function executeParent() {
    alert('Div Clicked');
  }
</script>

Hope these examples are clear. Try executing all these examples in a html file to see how they work.

rmcsharry
  • 5,363
  • 6
  • 65
  • 108
Keval Bhatt
  • 6,224
  • 2
  • 24
  • 40
  • 1
    in the first example, why does the parent's function run first before the child? if it the event has to propagate up, shouldn't it hit the child's function first? or does the event go up as part of the stack and resolve downwards, resulting in the parent's function to be run first and then the child? – akantoword Jul 26 '16 at 21:13
  • 2
    return false from the event handler doesn't do event.stopPropagation() apparently from this [codepen here](http://codepen.io/paramsinghvc/pen/ENOvLE?editors=1011) @keval-bhatt – Param Singh Dec 15 '16 at 08:41
  • 3
    -1, `return false` seems not to do (in FF51, Chrome 56 and IE11, where I tried) those things you mention. Please add references to back it up. – Sz. Feb 13 '17 at 14:46
  • The explanation of preventDefault() vs stopPropagation() is excellent (I finally understand!), but the `return false` part seems to be wrong as @Sz points out-- still observed on chrome 58. See http://stackoverflow.com/questions/128923/whats-the-effect-of-adding-return-false-to-a-click-event-listener#answer-128966 – Don Hatch Apr 24 '17 at 10:10
  • @DonHatch Now check I removed jquery and updated answer with pure javascript now take look for return false example. – Keval Bhatt Apr 25 '17 at 09:24
  • @akantoword in old example i mixed jquery and javascript both to gather so that is why its not working correctly now you can check the example your will get clear idea. – Keval Bhatt Apr 25 '17 at 09:26
  • Thanks, I think the problem is corrected, your explanation is great and really thorough, and these examples are a great way to answer a question so people can readily verify your claims. Changing to +1, very nice! – Don Hatch Apr 26 '17 at 08:31
  • 4
    This answer is no longer correct: https://jsfiddle.net/ts0t9yLa/ – Benjamin Gruenbaum Jun 04 '17 at 09:25
  • @BenjaminGruenbaum have you read example 5.2 and 5.3? In example 5.2 I mention return false will work for jquery event, and example 5.3 is same as what you posted. Let me know if you have any question? – Keval Bhatt Jul 03 '17 at 04:27
  • 1
    Yes, 5.1 and 5.3 don't work anymore (that's the fiddle). I __definitely__ remember them working or am confused about it :) – Benjamin Gruenbaum Jul 03 '17 at 09:24
  • It's hard to tell what this answer is talking about-- first it claims that "return false" means a certain thing, but then it says that's different from "normal" (non-jquery) behavior... but we were never talking about jquery to begin with, as far as I can see. – Don Hatch Dec 26 '22 at 20:23