Skip to content

Commit 6373944

Browse files
author
Kyle Bradshaw
committed
25 event capture/bubbling kb
1 parent dd8a5dc commit 6373944

File tree

1 file changed

+13
-1
lines changed
  • 25 - Event Capture, Propagation, Bubbling and Once

1 file changed

+13
-1
lines changed

25 - Event Capture, Propagation, Bubbling and Once/index-START.html renamed to 25 - Event Capture, Propagation, Bubbling and Once/index.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8">
55
<title>Understanding JavaScript's Capture</title>
66
</head>
7-
<body class="bod">
7+
<body class="body">
88

99
<div class="one">
1010
<div class="two">
@@ -39,7 +39,19 @@
3939

4040
<button></button>
4141
<script>
42+
//event capture goes top down, event bubbling goes up
43+
const divs = document.querySelectorAll('div');
4244

45+
function logText(e) {
46+
// e.stopPropagation(); //stop bubbling
47+
console.log(this.classList.value);
48+
}
49+
50+
// document.body.addEventListener('click', logText);
51+
52+
divs.forEach(div => div.addEventListener('click', logText, { capture: false, //true, goes top down
53+
once: true //listen for the click and then unbind itself (for future clicks)
54+
}));
4355
</script>
4456
</body>
4557
</html>

0 commit comments

Comments
 (0)