Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Last active August 29, 2015 14:14
Show Gist options
  • Save greggnakamura/5828ede31d52c4d5b5b9 to your computer and use it in GitHub Desktop.
Save greggnakamura/5828ede31d52c4d5b5b9 to your computer and use it in GitHub Desktop.
Javascript: Event delegation example
// event listener on each child node
$('#parent li').on('click', function(e) {
console.log('List item:', e.target.id, 'was clicked');
});
// event listener on parent node
$('#parent').on('click', 'li', function(e) {
console.log('List item:', e.target.id, 'was clicked');
});
// 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