Someone asked kirupaBot this question over on kirupa.com:
How does event delegation work in JavaScript?
kirupaBot’s answer
Great question! Event delegation is a handy pattern in JavaScript that leverages how events bubble up through the DOM so you can handle events efficiently.
Intuition
- When you listen for an event on a parent element, events that originate from its child elements bubble up to that parent. Instead of attaching listeners to many children, you attach one listener to a common ancestor and handle events based on the
event.target. - This gives you better performance (fewer listeners) and works well for dynamically added elements.
How it works (conceptual)
- You place a single event listener on a container (or document) for the event type you care about (e.g., click).
- Inside the handler, you inspect the actual element that fired the event (the
event.target). - You decide if you want to react based on attributes, classes, or
tagNames, and then run your logic accordingly.
What you’ll typically do
- Use
addEventListeneron a common ancestor. - In the handler, check if
event.targetmatches the selector you care about (or use closest to find an ancestor that matches).
Small example
- Suppose you have a list with many items and you want to respond to clicks on any item, even ones added later.
HTML:
<ul id="todoList">
<li>Item 1</li>
<li>Item 2</li>
<!-- more items -->
</ul>
JavaScript:
const list = document.getElementById('todoList');
list.addEventListener('click', function(event) {
// Find the closest li that was clicked (in case a child inside an li was clicked)
const item = event.target.closest('li');
if (item && list.contains(item)) {
console.log('Clicked item:', item.textContent);
// handle the item click
}
});
Notes
- Works with many event types: click, dblclick, keydown, mouseover, etc. It relies on event bubbling as described in the Event Capturing and Bubbling material [2].
- If you’re creating custom events or need more decoupling, you can still combine delegation with custom events [3].
If you want, I can tailor a small snippet to your exact DOM structure or walk through a concrete scenario you’re dealing with.
Cited concepts:
- Event bubbling and how events propagate to ancestors [2].
- Delegation pattern and using event targets to determine the actual element interacted with [2].
- General events overview as context [4].
- Custom events context if you’re mixing with dynamically created elements [3].
Sources from kirupa.com
- [1] Mouse Events in JavaScript
- [2] Event Capturing and Bubbling in JavaScript
- [3] Custom Events in JavaScript
- [4] Events in JavaScript
This topic continues a kirupaBot chat so real humans can weigh in. Did kirupaBot get it right? What would you add or explain differently? Reply below!