An event propagates or bubbles till the window object-level every time a registered event is called. For example, Let us consider a parent div element (“Div Parent”) that contains another child div element (“Div child”), and for both, a click event is registered. If child div is clicked, the event will be fired at all places i.e, at both parent and child objects.
StopPropagation() event method: Prevents propagation of any handlers at top-level DOM hierarchy to execute. It stops the click event from bubbling to the parent elements.
Example: In this method, after clicking the <div> element 1st event handler will occur after that nothing will happen. If you click the <p> element then the 2nd and 1st event handler will occur because <p> element is inside of <div> element but if you click <span> element only the 3rd event handler will occur, cause StopPropagation() event method stops the event from bubbling to the parent elements.
<!DOCTYPE html> <html> <head> <script src= </script> <script> $(document).ready(function() { $("span").click(function(event) { event.stopPropagation(); alert("The span element was clicked."); }); $("p").click(function(event) { alert("The p element was clicked."); }); $("div").click(function() { alert("The div element was clicked."); }); }); </script> <style> div { height: 120px; width: 300px; padding: 10px; margin: 50px; border: 2px solid black; background-color: purple; } p { background-color: orange; } span { background-color: cyan; } </style> </head> <body> <center> <div> <h1 style="color:lightgreen;"> GeeskforGeeks </h1> <p> Acomputer Science Portal for Geeks<br> <span>Click on this span element.</span> </p> </div> <p> event.stopPropagation() stops the click event from bubbling to the parent elements. </p> </center> </body> </html> |
Output:
- Before Clicking the element:

- After Clicking the <div> element:

- After Clicking the <p> element:


- After Clicking the <span> element:

StopImmediatePropagation() event method: Prevents both propagation of any other handlers and those at top level DOM hierarchy. It stops the other events which were assigned after this event.
Example: The StopImmediatePropagation() event stops the next event.
<!DOCTYPE html> <html> <head> <script src= </script> <script> $(document).ready(function() { $("div").click(function(event) { alert("'Event handler 1' executed"); event.stopImmediatePropagation(); }); $("div").click(function(event) { alert("'Event handler 2' executed"); }); $("div").click(function(event) { alert("'Event handler 3' executed"); }); }); </script> <style> div { height: 100px; width: 300px; padding: 10px; margin: 50px; border: 2px solid black; background-color: purple; } </style> </head> <body> <center> <div> <h1 style="color:lightgreen;"> GeeskforGeeks </h1> Click on this div element. </div> <p> event.stopImmediatePropagation() stop the second and third event. </p> </center> </body> </html> |
Output:
- Before Clicking the div element:

- After clicking the div element:

stopPropagation vs stopImmediatePropagation
| stopPropagation | stopImmediatePropagation |
|---|---|
| It will allow other handlers on the same element to be executed, prevent handlers on parent elements from running. | It will prevents every event from running. |
| It will allow more than one handeler to be executed one by one. | It will depend on you where you used this, that handeler will be the last one to be executed. |
| If you create a table containing <table>, <tr> and <td>. If you set three event handeler for <td> then other two event handeler will also run with this one. | But in this case if you do the same things the other two event handelers won’t run. |
Recommended Posts:
- jQuery | event.stopImmediatePropagation() Method
- HTML | DOM stopPropagation() Event Method
- jQuery | event.stopPropagation() Method
- Difference between == and === operator in JavaScript
- Difference between TypeScript and JavaScript
- Difference between var and let in JavaScript
- Difference between Java and JavaScript
- Difference between JavaScript and Php
- What is the difference between call and apply in JavaScript?
- Difference between substr() and substring() in JavaScript
- JavaScript | Difference between String.slice and String.substring
- Difference between function expression vs declaration in JavaScript
- Difference between != and !== operator in JavaScript
- What is the difference between children and childNodes in JavaScript?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- Difference between DOM parentNode and parentElement in JavaScript
- Get the relative timestamp difference between dates in JavaScript
- What is the difference between freeze and seal in JavaScript ?
- Difference between array.size() and array.length in JavaScript
- Difference between JavaScript and C Sharp
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


