I have a html element with an onclick attribute and I need to prevent that event from bubbling up. I tried doing this:
<div onclick="event.stopPropagation();">
and this:
<div onclick="(function(event){event.stopPropagation();})();)">
But neither works. I absolutely need to do this in the html onclick attribute since this div is part of a Razor partial view and the script is set on the ViewData dictionary. How do I call event.stopPropagation() on the html onclick attribute?
Edit: the error in this case is that event is null. So for some reason, I can´t access the event like this.
Use event.stopPropagation method of Event. There is a mistake in your second code snippet where you do not pass event to the anonymous function. Fixed code:
<div onclick="(function(e) { e.preventDefault(); e.stopPropagation(); })(event)">
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