Skip to content

Commit de8934e

Browse files
committed
Ex25 - Events capture, propagation, once
1 parent 83d4072 commit de8934e

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules/
33
*.log
44
haters/
5+
.vscode
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Understanding JavaScript's Capture</title>
6+
</head>
7+
<body class="bod">
8+
9+
<div class="one">
10+
<div class="two">
11+
<div class="three">
12+
</div>
13+
</div>
14+
</div>
15+
*
16+
<style>
17+
html {
18+
box-sizing: border-box;
19+
}
20+
*, *:before, *:after { box-sizing: inherit; }
21+
22+
div {
23+
width:100%;
24+
padding:100px;
25+
}
26+
27+
.one {
28+
background: thistle;
29+
}
30+
31+
.two {
32+
background:mistyrose;
33+
}
34+
35+
.three {
36+
background:coral;
37+
}
38+
</style>
39+
40+
<button></button>
41+
42+
<script src="script.js" charset="utf-8"></script>
43+
44+
</body>
45+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const divs = document.querySelectorAll('div');
4+
5+
function logText(event) {
6+
console.log(this.classList.value);
7+
// stop the propagation of the event
8+
event.stopPropagation();
9+
}
10+
11+
divs.forEach(div => div.addEventListener('click', logText, {
12+
capture: false, // by default it's false (events are bubbling up)
13+
once: true // will listen for a click once and then unbind itself (removeEventListener)
14+
}));

0 commit comments

Comments
 (0)