I\'m trying to create one event listener to handle all my \"clicks\" by putting it on the body and doing some event delegation. A simple example of what i\'m trying to do i
Here's with jquery for reference:
$("li").on('click', function(){
var y = this.parentElement.parentElement.id;
if (y === 'div1')
alert(y);
})
This event is attached to all Alternatively: However, this fires on every click within any elements within the Dom. You can follow the tree towards the root with
parentElement
to access the containing
id
property.
$("div").on('click', function(){
var y = this.id;
if (y === 'div1')
alert(y);
})