Last active
October 28, 2021 14:34
-
-
Save technikhil314/0745b1e0fd1ce232ed6cf8adeaf0c62c to your computer and use it in GitHub Desktop.
event capture, target, bubble with propagation explained
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html id="html"> | |
<head> | |
<title>Page Title</title> | |
</head> | |
<body id="body"> | |
<div id="one"> | |
<div id="two"> | |
<div id="three"> | |
<div id="four"> | |
<button id="button">Submit</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
<script> | |
function someFunc(event) { | |
// event.stopPropagation(); | |
// event.stopImmediatePropagation(); | |
console.log("capture", event.currentTarget, event.target, event); | |
} | |
function captureFunc(event) { | |
// event.stopPropagation(); | |
console.log("captureFunc", event.currentTarget, event.target, event); | |
} | |
function someOtherFunc(evnet) { | |
console.log("bubble", event.currentTarget, event.target, event); | |
} | |
const listOfAllIds = [ | |
"html", | |
"body", | |
"one", | |
"two", | |
"three", | |
"four", | |
"button" | |
]; | |
listOfAllIds.forEach(x => document.getElementById(x).addEventListener('click', someFunc, true)); | |
listOfAllIds.forEach(x => document.getElementById(x).addEventListener('click', captureFunc, true)); | |
listOfAllIds.forEach(x => document.getElementById(x).addEventListener('click', someOtherFunc)); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment