I don’t have an example at hand, but in some situations calling event.preventDefault() lets the original event through (navigating to page, submitting form etc) but returning false helps. What could cause this?
I don’t have an example at hand, but in some situations calling event.preventDefault() lets
Share
You don’t have an example to hand? OK, let me invent one that may or may not be whatever it was you were thinking of.
Remember that
return false;is the equivalent of calling bothevent.preventDefault();andevent.stopPropagation(). EDIT: This applies with jQuery, which explictly implements this behaviour and also normalisesevent.preventDefault()andevent.stopPropagation()for use in all browsers. It doesn’t work that way in all browsers with “plain” JS, in fact older IE versions don’t supportevent.preventDefault()at all, they have their own equivalentevent.returnValue = false;If you have nested elements and you handle the same event in several levels then calling
event.preventDefault()will not stop the outer elements’ event handlers from running, butreturn falsewill because it stops propagation of the event.An example that demonstrates it: http://jsfiddle.net/nnnnnn/KjLv3/
The alert will display. If you change the “a span” handler to return false the alert will not display.