Event bubbling and capturing
When you click an element on a page, the event does not just happen on that element. It travels through the DOM in three phases: down from the window to the target (capturing), at the target itself, and back up to the window (bubbling). Bubbling and capturing control which listeners run and in what order. Understanding them lets you handle events on many elements with a single listener and stop events from triggering things you did not intend.
The three phases in order
- Capturing phase: the event goes from
windowtodocument, then down through each ancestor towards the clicked element. - Target phase: the event reaches the element that was actually clicked.
- Bubbling phase: the event goes back up from the target through each ancestor to
window.
By default, addEventListener listens during bubbling. Passing { capture: true } (or true as the third argument) listens during capturing instead.
Watching the order happen
<div id="outer">
<div id="inner">
<button id="btn">Click me</button>
</div>
</div>
<script>
const log = (label) => () => console.log(label);
outer.addEventListener('click', log('outer capture'), { capture: true });
inner.addEventListener('click', log('inner capture'), { capture: true });
btn.addEventListener('click', log('button'));
inner.addEventListener('click', log('inner bubble'));
outer.addEventListener('click', log('outer bubble'));
// Clicking the button logs:
// outer capture, inner capture, button, inner bubble, outer bubble
</script>
Target versus currentTarget
Inside a listener, event.target is the element where the event started, such as the button or even a <span> inside it. event.currentTarget is the element whose listener is running right now. In the outer listener above, target is the button while currentTarget is outer.
Event delegation: one listener for many items
Because events bubble, you can put a single listener on a parent and work out which child was clicked. This handles items added later too, and avoids attaching hundreds of listeners.
const list = document.querySelector('#todo-list');
list.addEventListener('click', (event) => {
const button = event.target.closest('button.delete');
if (!button || !list.contains(button)) return;
button.closest('li').remove();
});
closest() walks up from the clicked element, so it still works if the user clicks an icon inside the button.
Stopping propagation, and why to be careful
event.stopPropagation() stops the event from moving to further elements. A typical use is a dropdown that closes when you click outside it: clicks inside the menu should not reach the document-level "close" listener.
The mistake is using it as a quick fix everywhere. Stopping propagation breaks other code that relies on bubbling, such as delegated handlers, analytics click tracking or a modal's outside-click logic, and the cause is hard to trace. Prefer checking event.target in the outer listener instead.
Also, stopPropagation() is not the same as preventDefault(). The first stops travel through the DOM; the second cancels the browser's default action, like following a link or submitting a form. You often need only one of them.
Some events do not bubble at all, such as focus, blur, mouseenter and mouseleave. For delegation, use their bubbling cousins: focusin, focusout, mouseover and mouseout.
Key points
- Events capture down, hit the target, then bubble up.
- Listeners default to the bubbling phase.
- Use
targetfor where it started andcurrentTargetfor where you are. - Delegate with one parent listener and
closest(). - Use
stopPropagation()sparingly.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.