I was answering a question here in SO, and I stumbled on an odd behavior. Check this demo
It's working great. I'm adding event handlers and when I click the image I have
alert(2)
without firing the link handler alert(1)
. The odd part is that, if I remove
the preventDefault, alert(1)
still doesn't fire, but it follows the link.
How come that stopPropagation, prevents the handler from bubbling the event, but it needs preventDefault to not follow the link?
This is purely for an educational reason. I just want to understand what's happening.
//EDIT please see the demo before answering. I have handlers on two different elements. Although stopPropagation prevents the handler of the parent element to fire, it doesn't prevent it from following the link. But doesn't stopPropagation, prevent it the event from bubbling? Doesn't it nullify the event for the parent element?
//Why the event is nullified for the handler alert(1) but not for the following of the link?