Clean β’ Professional
In the HTML DOM, when an event occurs on an element, it doesnβt just happen in isolation. The event can propagate through the DOM tree, either from the target element up to its ancestors (bubbling) or from the root down to the target (capturing). Understanding this concept is essential for precise event handling.
Bubbling is the default behavior in most browsers.
Example:
<div id="parent">
<button id="child">Click Me</button>
</div>
<script>
const parent = document.getElementById("parent");
const child = document.getElementById("child");
child.addEventListener("click", () => {
console.log("Child clicked!");
});
parent.addEventListener("click", () => {
console.log("Parent clicked!"); // Fires after child
});
</script>
Capturing is the opposite of bubbling.
Example:
parent.addEventListener("click", () => {
console.log("Parent clicked!");
}, true); // true β capturing phase
child.addEventListener("click", () => {
console.log("Child clicked!");
});
Stop Bubbling
child.addEventListener("click", (event) => {
console.log("Child clicked!");
event.stopPropagation(); // Prevents event from reaching parent
});
Stop Capturing
stopPropagation() works for both phases.capture: true)stopPropagation() to prevent unintended event triggering.addEventListener() allows choosing phase via the third parameter:element.addEventListener("click", handler, { capture: true });
Real-World Example
<div id="outer" style="padding:20px; background:lightblue;">
Outer
<div id="inner" style="padding:20px; background:lightgreen;">
Inner
<button id="btn">Click</button>
</div>
</div>
<script>
const outer = document.getElementById("outer");
const inner = document.getElementById("inner");
const btn = document.getElementById("btn");
// Bubbling
outer.addEventListener("click", () => console.log("Outer clicked!"));
inner.addEventListener("click", () => console.log("Inner clicked!"));
btn.addEventListener("click", () => console.log("Button clicked!"));
// Capturing example
outer.addEventListener("click", () => console.log("Outer capturing!"), true);
inner.addEventListener("click", () => console.log("Inner capturing!"), true);
</script>Β