Skip to content

Instantly share code, notes, and snippets.

@sranka23
Created September 9, 2021 17:57
Show Gist options
  • Save sranka23/59cd9a38d4f2a987ea6d8e697c5a8c7b to your computer and use it in GitHub Desktop.
Save sranka23/59cd9a38d4f2a987ea6d8e697c5a8c7b to your computer and use it in GitHub Desktop.
Event Bubbling and Event Capturing
/*
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")
});
@sranka23
Copy link
Author

sranka23 commented Sep 9, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment