Skip to content

Instantly share code, notes, and snippets.

@technikhil314
Last active October 28, 2021 14:34
Show Gist options
  • Save technikhil314/0745b1e0fd1ce232ed6cf8adeaf0c62c to your computer and use it in GitHub Desktop.
Save technikhil314/0745b1e0fd1ce232ed6cf8adeaf0c62c to your computer and use it in GitHub Desktop.
event capture, target, bubble with propagation explained
<!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