Home >>jQuery Tutorial >jQuery event.stopPropagation() Method

jQuery event.stopPropagation() Method

jQuery event.stopPropagation() Method

jQuery event.stopPropagation() method in jQuery is used to stops the bubbling of an event to parent elements (preventing any parent event handlers from being executed)

Syntax:
event.stopPropagation()

Parameter Values

Parameter Description
event It is a required parameter and is used to specify the event parameter comes from the event binding function
Here is an Example of jQuery event.stopPropagation() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".box1").click(function(event){
event.stopPropagation();
alert("The span element was clicked.");
});
$(".txt").click(function(event){
alert("The p element was clicked.");
});
$(".box").click(function(){
alert("The div element was clicked.");
});
});
</script>
</head>
<body>
<div class="box" style="height:80px;width:400px;padding:8px;background-color:orange;">
This is a div element.
<p class="txt" style="background-color:lightgreen">This is a p element, in the div element. <br>
<span class="box1" style="background-color:white">This is a span element in the p and the div element.</span></p></div>
</body>
</html>

Output:
This is a div element.

This is a p element, in the div element.
This is a span element in the p and the div element.