Consider the following:
<div onclick="alert('you clicked the header')" class="header">
<span onclick="alert('you clicked inside the header');">something inside the header</span>
</div>
How can I make it so that when the user clicks the span, it does not fire the div
's click event?
There are two ways to get the event object from inside a function:
If you need to support legacy browsers that don't follow the W3C recommendations, generally inside a function you would use something like the following:
which would check first one, and then the other and store whichever was found inside the event variable. However in an inline event handler there isn't an
e
object to use. In that case you have to take advantage of thearguments
collection which is always available and refers to the complete set of arguments passed to a function:However, generally speaking you should be avoiding inline event handlers if you need to to anything complicated like stopping propagation. Writing your event handlers separately and the attaching them to elements is a much better idea in the medium and long term, both for readability and maintainability.
Use this function, it will test for the existence of the correct method.
I had the same issue - js error box in IE - this works fine in all browsers as far as I can see (event.cancelBubble=true does the job in IE)
According to this page, in IE you need:
event.cancelBubble = true
For ASP.NET web pages (not MVC), you can use
Sys.UI.DomEvent
object as wrapper of native event.or, pass event as a parameter to inner function:
and in someFunction:
Use event.stopPropagation().
For IE:
window.event.cancelBubble = true