Event Bubbling as the Foundation
You'll learn why event delegation depends entirely on event bubbling to function.
Why Delegation Needs Bubbling
Event delegation works because of event bubbling — the process where events travel upward through the DOM tree from the target element to its ancestors. Without bubbling, delegation would be impossible.
When you click a child element, the event doesn't just fire on that element and stop. It "bubbles up" through each parent, grandparent, and so on, all the way to the document and window objects. Event delegation takes advantage of this journey.
How Bubbling Enables Delegation
Imagine you have a list with hundreds of items. Instead of attaching a listener to each <li>, you attach one listener to the parent <ul>. When someone clicks an <li>, the click event bubbles up from the <li> to the <ul>, where your delegated listener catches it.
The delegated listener can then use event.target to identify which specific child was actually clicked, even though the listener is attached to an ancestor.
The Travel Path
Here's the journey:
- User clicks
<li> - Click event fires on the
<li>(target) - Event bubbles to
<ul>(where your listener waits) - Your listener executes
- Event continues bubbling to
<body>,<html>,document,window
Without this upward travel, the parent <ul> would never "hear about" clicks happening on its children. Bubbling is the messenger that carries event information from deep within the DOM tree to listening ancestors above.
Key Takeaway: Event delegation relies on event bubbling — the mechanism that lets events travel from the target element up through all its ancestors, allowing parent elements to handle events that occur on their descendants.