javascript e.preventDefault() and return false

Hello,

In javascript; When I want to prevent other event handlers from executing after a certain event is fired, I can use one of two techniques; e.preventDefault() or return false.

So what is the difference in between e.preventDefault() and return false and When to use return false and when preventDefault()?

First we need to understand below things
1. e.preventDefault() = It stops the browsers default behaviour
2. e.stopPropagation() = It prevents the event from propagating.

Return False :
when we call return false in our function then it does below things
1. e.preventDefault()
2. e.stopPropagation()
3. Stops the callback execution and returns immediately

e.preventDefault() :
when we call e.preventDefault() in our function then it does only one thing i.e e.preventDefault()

In short return false takes things a bit further in, it also prevents that event from propagating (or “bubbling up”) the DOM.
function()
{
return false;
}

is euqal to

function(e)
{
e.preventDefault();
e.stopPropagation();
}

Hope this will help you in understanding e.preventdefault() and return false;

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>