Event capturing vs bubbling in JavaScript
Intermediate · 3 min read
Most of the time you can think of an event as starting where you clicked and bubbling up. That's only the second half of its journey. Before it reaches the element you clicked, it travels down to it from the top of the page. That first half is called the capture phase, and once in a while it's exactly what you need.
In this tutorial you will add listeners to three nested elements in both phases and print the full order, then use a capture listener to stop a click before its target ever sees it. You should have used addEventListener and know what bubbling is. If bubbling is new to you, the bubbling exercise linked at the end is a good place to start.
An event's three phases
Every event makes one round trip:
- Capture: from the top of the page down through each ancestor, outermost first, toward the target.
- Target: at the element where the event happened.
- Bubble: back up from the target through each ancestor, innermost first.
A normal addEventListener('click', fn) listens in the bubble phase. To listen in the capture phase, pass a third argument of true, or an options object { capture: true }.
Here are the nested elements:
<div id="outer">
outer
<div id="inner">
inner
<button id="target" type="button">target</button>
</div>
</div>
Here is the exercise. Try it yourself first, or read on and we will build it.
14 The capture phase
fairAdd listeners to a parent and a child in both phases, and show the full order: down the tree, at the target, then back up.
Stuck? Three hints, smallest first
- 1. A third argument of
trueregisters for the capture phase. - 2. Capture runs outermost first — the opposite of bubbling.
- 3. On the target element itself, listeners run in the order they were added, regardless of phase.
Step 1: bubble listeners only
const target = document.getElementById('target');
const inner = document.getElementById('inner');
const outer = document.getElementById('outer');
const log = [];
target.addEventListener('click', () => log.push('target bubble'));
inner.addEventListener('click', () => log.push('inner bubble'));
outer.addEventListener('click', () => log.push('outer bubble'));
target.dispatchEvent(new Event('click', { bubbles: true }));
console.log(log); // ['target bubble', 'inner bubble', 'outer bubble']
This is the familiar order: the target first, then outward.
Step 2: add capture listeners
const target = document.getElementById('target');
const inner = document.getElementById('inner');
const outer = document.getElementById('outer');
const log = [];
outer.addEventListener('click', () => log.push('outer capture'), true);
inner.addEventListener('click', () => log.push('inner capture'), true);
target.addEventListener('click', () => log.push('target capture'), true);
target.addEventListener('click', () => log.push('target bubble'));
inner.addEventListener('click', () => log.push('inner bubble'));
outer.addEventListener('click', () => log.push('outer bubble'));
target.dispatchEvent(new Event('click', { bubbles: true }));
for (const line of log) console.log(line);
// outer capture
// inner capture
// target capture
// target bubble
// inner bubble
// outer bubble
Read the six lines from top to bottom and you can see the whole trip: down through outer and inner, the two listeners on the target, then back up. The capture half is the bubble half in reverse.
On the target itself, the capture listener was added first and ran first.
Step 3: stop a click before it arrives
A capture listener on a parent runs before anything inside it. That means it can stop the event before the target hears it.
const wrap = document.createElement('div');
const b = document.createElement('button');
wrap.appendChild(b);
document.body.appendChild(wrap);
const blocked = [];
wrap.addEventListener('click', (event) => {
blocked.push('wrapper capture');
event.stopPropagation();
}, true);
b.addEventListener('click', () => blocked.push('button'));
b.dispatchEvent(new Event('click', { bubbles: true }));
console.log(blocked); // ['wrapper capture']
The button never heard its own click. This is useful when a whole area should ignore clicks for a while, such as a form that is busy saving: one capture listener on the container can block everything inside.
Capture also helps with events that don't bubble. focus is one: it fires on the field that gets focus, and doesn't bubble up. A capture listener on a parent still sees it on the way down.
Step 4: removing a capture listener
Capture and bubble listeners are stored separately. To remove a capture listener, you must say true again:
const outer = document.getElementById('outer');
const target = document.getElementById('target');
const log = [];
const fn = () => log.push('outer capture');
outer.addEventListener('click', fn, true);
outer.removeEventListener('click', fn); // no third argument
target.dispatchEvent(new Event('click', { bubbles: true }));
console.log(log); // ['outer capture']: still there
outer.removeEventListener('click', fn, true);
log.length = 0;
target.dispatchEvent(new Event('click', { bubbles: true }));
console.log(log.length); // 0
The first removeEventListener looked for a bubble listener called fn, found none, and quietly did nothing. With true, it removes the capture one. No error appears in either case, which is why this one is easy to miss.
The full solution
The panel registers all six listeners and clicks the target, then shows a capture listener blocking a button. Its checks confirm capture runs outermost first, then the target, then bubbling outward, six handlers in total, the capture order is the exact reverse of the bubble order, the blocked button never ran, true and { capture: true } both select capture, and the two kinds are removed separately. Press Run, then click the target in the live demo.
allow-scripts only, so it cannot reach this page.const target = el('button', { id: 'target', type: 'button' }, 'target');
const inner = el('div', { id: 'inner' }, ['inner', target]);
const outer = el('div', { id: 'outer' }, ['outer', inner]);
document.body.appendChild(outer);
const log = [];
// The third argument, true, means "capture": run on the way DOWN.
outer.addEventListener('click', () => log.push('outer capture'), true);
inner.addEventListener('click', () => log.push('inner capture'), true);
// On the target itself both are registered, and they run in the order added.
target.addEventListener('click', () => log.push('target capture'), true);
target.addEventListener('click', () => log.push('target bubble'));
// No third argument means bubble: run on the way back UP.
inner.addEventListener('click', () => log.push('inner bubble'));
outer.addEventListener('click', () => log.push('outer bubble'));
fire(target, 'click');
for (const line of log) console.log(line);
// A capture listener can stop the event before the target ever sees it.
const blocked = [];
const b = el('button', { type: 'button' }, 'blocked');
const wrap = el('div', {}, b);
document.body.appendChild(wrap);
wrap.addEventListener('click', (e) => { blocked.push('wrapper capture'); e.stopPropagation(); }, true);
b.addEventListener('click', () => blocked.push('button'));
fire(b, 'click');
console.log('capture can stop it early:', blocked, '<- the button never heard its own click');
Output
11 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 11 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('capture runs outermost first', log[0], 'outer capture');
test('then inwards', log[1], 'inner capture');
test('then the target', log.slice(2, 4), ['target capture', 'target bubble']);
test('then bubbling outwards', log.slice(4), ['inner bubble', 'outer bubble']);
test('six handlers ran in total', log.length, 6);
test('capture is the exact reverse of bubble', [log[0], log[1]], [log.at(-1), log.at(-2)].map((s) => s.replace('bubble', 'capture')));
test('a capture listener can stop the event before the target', blocked, ['wrapper capture']);
test('so the target never ran', blocked.includes('button'), false);
test('the third argument true is what selects capture', (() => { const log2 = []; const p = el('div', {}, el('button', {}, 'x')); document.body.appendChild(p); p.addEventListener('click', () => log2.push('cap'), true); p.addEventListener('click', () => log2.push('bub'), false); fire(p.children[0], 'click'); return log2; })(), ['cap', 'bub']);
test('an options object works too', (() => { const log3 = []; const p = el('div', {}, el('button', {}, 'x')); document.body.appendChild(p); p.addEventListener('click', () => log3.push('cap'), { capture: true }); fire(p.children[0], 'click'); return log3; })(), ['cap']);
test('capture and bubble listeners are removed separately', (() => { const fn = () => {}; const d = el('div', {}); d.addEventListener('click', fn, true); d.addEventListener('click', fn, false); d.removeEventListener('click', fn, true); return d.listenerCount('click'); })(), 1);
Every event makes three passes: capture from the document down to the target, the target itself, then bubble back up. Most code only ever uses the bubble phase, which is why the capture phase feels obscure — but it is the same journey, just the first half.
Two places it earns its keep. A capture listener on an ancestor sees the event before the target does, so it can block it entirely — the assertion shows a button never receiving its own click. And it is the only way to intercept events that do not bubble, like focus, from a parent.
The detail that catches people: capture and bubble registrations are stored separately, so removeEventListener(type, fn) without the matching third argument does not remove a capture listener. The last assertion checks that one listener survives after removing only the capture one.
Common mistakes
- Forgetting
truewhen removing.removeEventListener(type, fn)doesn't remove a listener added withtrue. Pass the same flag. - Expecting capture to run inside-out. Capture runs outermost first, the opposite of bubbling.
- Using capture everywhere. Most code belongs in the bubble phase. Reach for capture when a parent must act first or the event doesn't bubble.
Try it yourself
- Register the outer capture listener with
{ capture: true }instead oftrue, and check the order doesn't change. - Log
event.eventPhasein each listener. Capture is 1, at the target is 2, and bubbling is 3. - Add a "busy" checkbox. While it's ticked, a capture listener on
outerstops every click inside it.