$("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.
preventDefault() prevents the default browser behavior for a given element. stopPropagation() stops an event from bubbling or propagating up the DOM tree. Whereas, return false is a combination of both preventDefault() and stopPropagation() .
e. preventDefault() will prevent the default event from occuring, e. stopPropagation() will prevent the event from bubbling up and return false will do both. Note that this behaviour differs from normal (non-jQuery) event handlers, in which, notably, return false does not stop the event from bubbling up.
The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.
There is no opposite method of event. preventDefault() to understand why you first have to look into what event. preventDefault() does when you call it. Underneath the hood, the functionality for preventDefault is essentially calling a return false which halts any further execution.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With