Created
September 9, 2021 17:57
-
-
Save sranka23/59cd9a38d4f2a987ea6d8e697c5a8c7b to your computer and use it in GitHub Desktop.
Event Bubbling and Event Capturing
This file contains 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
/* | |
Author: Shourya Ranka | |
Objective: Demonstrate event bubbling and capturing | |
*/ | |
// event binding on parent <div> is captured by child elements too. Clicking on inner divs will pick this event. | |
document.querySelector('#target1').addEventListener('click', function(e){ | |
console.log(e.target.id); | |
},true); | |
// below three events indicate event bubbling, any event triggered on the child element will be bubbled up to parent too. | |
document.querySelector('#target1').addEventListener('click', function(e){ | |
console.log("outermost item clicked") | |
}); | |
document.querySelector('#target2').addEventListener('click', function(e){ | |
console.log("middle div clicked") | |
}); | |
document.querySelector('#target3').addEventListener('click', function(e){ | |
console.log("innermost div clicked") | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code Execution @ https://codepen.io/sranka2310/pen/VwWpyPJ