Last active
August 29, 2015 14:14
-
-
Save greggnakamura/5828ede31d52c4d5b5b9 to your computer and use it in GitHub Desktop.
Javascript: Event delegation example
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
// event listener on each child node | |
$('#parent li').on('click', function(e) { | |
console.log('List item:', e.target.id, 'was clicked'); | |
}); |
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
// event listener on parent node | |
$('#parent').on('click', 'li', function(e) { | |
console.log('List item:', e.target.id, 'was clicked'); | |
}); |
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
// event listener on parent node | |
document.getElementById('parent').addEventListener('click', function(e) { | |
if (e.target && e.target.nodeName === 'LI') { | |
console.log('List item:', e.target.id, 'was clicked'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment