Event stopPropagation Method in jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/Event_stopPropagation_Method.css">
<title>Event stopPropagation Method</title>
</head>
<body>
<h1>jQuery Event.stopPropagation()</h1>
<div id="box">
<h2>Test Box</h2>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Exercitationem facilis eaque cumque perspiciatis
sapiente distinctio aut, fugit ex, praesentium officia et quod, sit reprehenderit dolorem unde neque. Sunt,
ut amet.</p>
<button>Test Button</button>
</div>
<script src="js/jquery-3.7.1.min.js"></script>
<script>
//jQuery Event Object : Properties & Methods
// event.pageX,event.pageY,event.type,event.which,event.target,event.preventDefault(),event.isDefaultPrevented(),
// event.stopPropagation(),event.isPropagationStopped(),event.data()
$(document).ready(function () {
$("#box").click(function () {
alert("The div element was clicked");
});
$("h2").click(function () {
alert("The h2 element was clicked");
});
$("p").click(function (event) {
event.stopPropagation(); //To stop parent Event Trigger of "p" Tag
alert("The p element was clicked : " + event.isPropagationStopped());
//isPropagationStopped() checks whether stopPropagation() method is used OR Not
//if stopPropagation() method is used then isPropagationStopped() returns true otherwise false
});
$("button").click(function () {
alert("The button element was clicked");
});
});
</script>
</body>
</html>
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment