diagram.mmd — sequence
Event Bubbling sequence diagram

Event bubbling is the DOM phase during which an event, after firing on its target element, propagates upward through each ancestor element to the document root, giving every parent a chance to respond to the event.

When a user clicks a

Event bubbling is the DOM propagation phase during which an event, after firing on its target element, travels upward through each ancestor — from the target to `document` to `window` — giving every parent element a chance to respond with its own listeners.
Instead of attaching a listener to each child element (costly for large lists), you attach one listener to the common parent. When a child is clicked, the event bubbles to the parent, and the handler reads `event.target` to identify the originating element. This reduces memory, avoids re-attaching listeners when children are added or removed, and is how React's synthetic event system works internally.
Use `stopPropagation` sparingly — only when you have a specific, intentional reason to prevent outer elements from responding. Overuse silently breaks document-level analytics listeners, third-party overlays, and accessibility tools that rely on observing all events. Prefer filtering inside handlers with `event.target` checks instead of stopping propagation.
mermaid
sequenceDiagram participant User participant Button as button.submit participant Div as div.form-wrapper participant Main as main participant Body as body participant Doc as document User->>Button: Click note">Note over Button: Target phase — event fires on target Button->>Button: Run button click listeners note">Note over Button,Doc: Bubble phase — event propagates upward Button->>Div: Event bubbles to div.form-wrapper Div->>Div: Run div listeners (if any) Div->>Main: Event bubbles to main Main->>Main: Run main listeners (if any) Main->>Body: Event bubbles to body Body->>Body: Run body listeners (if any) Body->>Doc: Event bubbles to document Doc->>Doc: Run document listeners (event delegation here) alt stopPropagation called at any level note">Note over Button,Doc: Propagation halted — no further bubbling end
Copied to clipboard