Skip to content

Commit 33a77eb

Browse files
committed
Event Bubbling, Capturing and Propagation
1 parent 8fb8c67 commit 33a77eb

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Event Bubbling, Capturing and Propagation</title>
8+
</head>
9+
<body>
10+
<div id="output"></div>
11+
<script>
12+
const output = document.getElementById("output");
13+
14+
output.innerHTML = `
15+
<h1 id="title"> Event Bubbling, Capturing and Propagation</h1>
16+
<div class="one">
17+
18+
<div class="two">
19+
<p>dddd</p>
20+
<button type="button" id="btn" class="three">Click me plz</button>
21+
</div>
22+
</div>
23+
24+
25+
`;
26+
const one = document.querySelector(".one");
27+
const two = document.querySelector(".two");
28+
const three = document.querySelector("#btn");
29+
30+
function handleClick(e) {
31+
//stop event bubbling (bottom to top)
32+
e.stopPropagation();
33+
34+
e.stopImmediatePropagation();
35+
console.log(e.target);
36+
}
37+
one.addEventListener("click", handleClick);
38+
two.addEventListener("click", handleClick);
39+
three.addEventListener("click", handleClick);
40+
41+
three.addEventListener("click", () => {
42+
console.log("twice");
43+
});
44+
</script>
45+
</body>
46+
</html>

0 commit comments

Comments
 (0)