Skip to content

Commit d2ccf16

Browse files
author
tomgillard
committed
Completing javascript 30 day 25 in own index file
1 parent 0b589ce commit d2ccf16

File tree

1 file changed

+58
-0
lines changed
  • 25 - Event Capture, Propagation, Bubbling and Once

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Understanding JavaScript's Capture</title>
6+
<style>
7+
html {
8+
box-sizing: border-box;
9+
}
10+
*, *:before, *:after { box-sizing: inherit; }
11+
12+
div {
13+
width:100%;
14+
padding:100px;
15+
}
16+
17+
.one {
18+
background: thistle;
19+
}
20+
21+
.two {
22+
background:mistyrose;
23+
}
24+
25+
.three {
26+
background:coral;
27+
}
28+
</style>
29+
</head>
30+
<body class="bod">
31+
32+
<div class="one">
33+
<div class="two">
34+
<div class="three">
35+
</div>
36+
</div>
37+
</div>
38+
39+
<button></button>
40+
41+
<script>
42+
const divs = document.querySelectorAll('div');
43+
44+
const logText = function logText(e) {
45+
// e.stopPropagation();
46+
console.log(this.classList.value);
47+
};
48+
49+
// capture runs function on the way DOWN the DOM - defaults to false
50+
// once unbinds event listener after first time event listering is triggered
51+
divs.forEach(div => div.addEventListener('click', logText, {
52+
capture: false,
53+
once: true
54+
}));
55+
56+
</script>
57+
</body>
58+
</html>

0 commit comments

Comments
 (0)