Event bubbling and capturing
Click a link inside a box inside a page and three elements have been clicked at once. Follow the event down and back up to see which listener runs when, and how to stop it.
In 30 seconds
When you click an element, the browser does not tell only that element. The event travels down from the top of the page to the element (capture), then back up through every ancestor (bubble).
Listeners run in that order. By default they listen on the way up, so the innermost element hears the click first and the outermost last.
stopPropagation ends the journey early. preventDefault is a separate switch: it cancels the browser’s built-in action, such as following a link, and leaves the journey alone.
Event bubbling and capturing, step by step
The route really starts at window and also passes the html element; both are left out. A few events, such as focus and blur, do not bubble. stopPropagation still lets other listeners on the same element run.
- Step 1: One click, several elements. A link sits inside a div, which sits inside the body. A click on the link is therefore also a click inside the div and inside the body. Four listeners on this page are waiting for clicks. The browser has a fixed rule for who is told, and in what order.
- Step 2: Phase 1: capture, going down. The event starts at the top of the page and travels down towards the element that was clicked, passing every ancestor on the way. Only listeners registered with capture: true run during this descent. Here that is the first listener on the body.
- Step 3: Phase 2: at the target. The event reaches the innermost element under the pointer, the link. That element is the target, and its own listeners run now. event.target names it for the rest of the trip, whichever element’s listener is reading it.
- Step 4: Phase 3: bubble, going up. Now the event travels back up through the same ancestors, innermost first. Ordinary listeners, registered without the capture option, run in this phase. So the div hears the click before the body does.
- Step 5: The order, and who is who. One click, four calls: capture listeners from the outside in, then the target, then bubble listeners from the inside out. Inside each call, event.target is still the link, while event.currentTarget is the element whose listener is running. That is how one listener on a parent can serve many children.
- Step 6: stopPropagation ends the trip. A listener can call stopPropagation to end the journey where it stands. Here the div does so: the event goes no further and the body’s bubble listener never runs. The click itself is not undone. The browser still follows the link.
- Step 7: preventDefault is different. preventDefault says nothing about the journey. It tells the browser not to perform its own built-in action for the event: following a link, submitting a form, ticking a box. Here all four listeners still run, in the usual order, but the page does not navigate.
Try it yourself
Learn this first
Spotted a mistake on this page?