Category: javascript
Event delegation
Published on 16 Jul 2026
Explanation
Event delegation attaches one event listener to a parent instead of multiple child elements.
Code:
list.addEventListener('click', function(e) {
console.log(e.target);
});
Explanation
The event.target property identifies the clicked element.
Code:
console.log(event.target.tagName);
Explanation
Delegation improves performance for large lists.
Code:
parent.addEventListener('click', handler);
Explanation
New child elements automatically inherit delegated events.
Code:
parent.appendChild(newItem);
Explanation
Delegation reduces duplicate event listeners.
Code:
if(event.target.matches('li')){
console.log('Item');
}