Day 14 — Event Bubbling, Capturing, and Delegation (How Browser Events Actually Work)
“Most developers use events. Far fewer understand how events actually travel through the browser.”
Day 14 — Event Bubbling, Capturing, and Delegation (How Browser Events Actually Work)

“Most developers use events. Far fewer understand how events actually travel through the browser.”
By now, you’ve already seen how JavaScript can respond to user interactions using event listeners. A button gets clicked, a function runs, and the interface changes. On the surface, it feels simple.
But underneath that simplicity, the browser is doing something surprisingly sophisticated.
When an event happens inside the DOM, it does not simply “trigger a function.” The event actually moves through the page in multiple phases. It travels across elements, interacts with parent containers, and follows a propagation system that most beginners never notice initially.
Understanding this system is important because once applications become larger, event behavior starts becoming more complex. Without understanding propagation, events can feel random and difficult to debug.
This is the point where frontend development starts becoming more architectural.
Understanding the DOM Hierarchy
Before understanding events, you first need to remember that the DOM is structured like a tree.
Consider this HTML:
<div id="parent">
<button id="child">Click Me</button>
</div>
Here:
- the
buttonexists inside thediv - the
divis the parent - the
buttonis the child
This relationship matters because events move through this hierarchy when interactions occur.
When you click the button, the browser does not only know about the button. It also knows about every surrounding parent element.
That is where propagation begins.
What Is Event Bubbling?
Event bubbling is the default event behavior in JavaScript.
When an event happens on a child element, the event first runs on that element and then “bubbles up” through its parents.
Imagine clicking the button.
The event travels like this:
button → div → body → document
This means parent elements can react to events that happened inside their children.
Let’s see this in code.
<div id="parent">
<button id="child">Click Me</button>
</div>
const parent = document.querySelector("#parent");
const child = document.querySelector("#child");
parent.addEventListener("click", function () {
console.log("Parent clicked");
});
child.addEventListener("click", function () {
console.log("Child clicked");
});
Now click the button.
The output becomes:
Child clicked
Parent clicked
This surprises many beginners initially.
Why did the parent listener run even though the button was clicked?
Because the event bubbled upward through the DOM tree.
Why Bubbling Exists
At first, bubbling can feel unnecessary or confusing. But it exists for a very important reason.
The browser treats events as part of the entire interface hierarchy, not isolated elements. Bubbling allows parent containers to react to interactions happening inside them.
Without bubbling, many powerful frontend patterns would become difficult or inefficient.
This becomes especially important when discussing event delegation later.
Stopping Event Bubbling
Sometimes you do not want an event to continue traveling upward.
JavaScript allows you to stop propagation.
child.addEventListener("click", function (event) {
event.stopPropagation();
console.log("Child clicked");
});
Now clicking the button only logs:
Child clicked
The event never reaches the parent.
This is useful in situations like:
- dropdown menus
- modal windows
- nested clickable elements
where parent interactions should not trigger accidentally.
Event Capturing
So far, you’ve seen bubbling, which moves upward.
But events actually have another phase before bubbling even starts.
This phase is called capturing.
During capturing, the event travels downward through the DOM tree before reaching the target element.
The full flow looks like this:
document → body → div → button
then after reaching the target:
button → div → body → document
Most developers mainly work with bubbling because it is the default behavior. But capturing exists underneath every event system in the browser.
You can enable capturing manually:
parent.addEventListener(
"click",
function () {
console.log("Parent capturing");
},
true
);
That final true tells JavaScript to listen during the capturing phase instead of bubbling.
Now the parent listener runs before the child listener.
For beginners, the important thing is not memorizing phases perfectly. It is understanding that events travel through the DOM instead of instantly triggering in isolation.
Understanding the Event Object
Whenever an event occurs, JavaScript automatically provides an event object.
button.addEventListener("click", function (event) {
console.log(event);
});
This object contains information about the event.
For example:
- which element triggered it
- mouse position
- keyboard input
- event type
One of the most important properties is:
event.target
This tells you which element originally triggered the event.
Event Delegation
This is where things become extremely powerful.
Imagine you have 100 buttons.
You could attach 100 separate event listeners.
But that becomes inefficient quickly.
Instead, JavaScript allows you to attach a single listener to a parent element and detect which child triggered the event.
This technique is called event delegation.
<ul id="list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
const list = document.querySelector("#list");
list.addEventListener("click", function (event) {
console.log(event.target.textContent);
});
Now clicking any list item works automatically.
Why?
Because the event bubbles upward to the parent <ul> element, and the parent listener checks which child triggered the event.
This pattern is extremely common in real applications because it improves performance and simplifies code.
Why Delegation Matters
Event delegation becomes especially important when elements are created dynamically.
Imagine adding new list items using JavaScript.
Without delegation, you would need to manually attach listeners every time a new element appears.
With delegation, the parent listener already handles everything automatically.
This is one of the first frontend patterns that starts feeling genuinely scalable.
A Common Beginner Mistake
One of the most common mistakes is forgetting that clicks on nested elements also bubble upward.
For example:
<button>
<span>Click</span>
</button>
Clicking the span still triggers the button’s event listener because the event travels upward.
This behavior confuses many beginners until they fully understand propagation.
A Better Mental Model
Instead of thinking:
“The button was clicked.”
Think:
“An event started at the button and traveled through the DOM.”
That mental model explains bubbling, capturing, and delegation naturally.
Why This Matters
Modern frontend applications rely heavily on event systems.
Menus, dropdowns, modals, drag-and-drop interfaces, keyboard shortcuts, interactive dashboards — all of them depend on event propagation behavior.
Frameworks like React also build abstraction layers around browser events, but underneath those abstractions, the browser’s propagation system still exists.
Understanding this system makes debugging frontend behavior dramatically easier.
Try This Yourself
Create nested elements and attach listeners to both parent and child elements.
Then:
- observe bubbling
- stop propagation
- experiment with capturing
- use
event.target - try event delegation
This topic becomes much easier once you actually watch events travel through the DOM yourself.
What’s Next?
In the next article, we’ll explore asynchronous JavaScript, where JavaScript starts handling timers, delayed execution, and non-blocking behavior using concepts like setTimeout, callbacks, promises, and eventually async/await.
“This is where JavaScript starts dealing with time itself.”
See you in Day 15.
메타데이터
- post_id
- d3be80bbb371
- slug
- day-14-event-bubbling-capturing-and-delegation-how-browser-events-actually-work-d3be80bbb371
- url
- https://medium.com/@techmuse007/day-14-event-bubbling-capturing-and-delegation-how-browser-events-actually-work-d3be80bbb371
- canonical_url
- https://medium.com/@techmuse007/day-14-event-bubbling-capturing-and-delegation-how-browser-events-actually-work-d3be80bbb371
- author_url
- https://medium.com/@techmuse007
- status
- ok
- fetched_at
- 2026-07-29 19:32:09