Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop event propagation with inline onclick attribute?

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?

like image 701
Sam Avatar asked Dec 22 '08 23:12

Sam


People also ask

How do I stop click event propagation?

To stop the click event from propagating to the <div> element, you have to call the stopPropagation() method in the event handler of the button: btn. addEventListener('click', (e) => { e. stopPropagation(); alert('The button was clicked!

How do you prevent event propagation?

The stopPropagation() method prevents propagation of the same event from being called. Propagation means bubbling up to parent elements or capturing down to child elements.

Which event can be used to stop event propagation in react?

14, returning false from an event handler will no longer stop event propagation. Instead, e. stopPropagation() or e. preventDefault() should be triggered manually, as appropriate.

How do you stop event propagation in typescript?

Try with (click)="$event. stopPropagation()" . It might help as it worked for me in my scenario.


1 Answers

Use event.stopPropagation().

<span onclick="event.stopPropagation(); alert('you clicked inside the header');">something inside the header</span> 

For IE: window.event.cancelBubble = true

<span onclick="window.event.cancelBubble = true; alert('you clicked inside the header');">something inside the header</span> 
like image 91
James Avatar answered Sep 28 '22 06:09

James