Event Bubbling is one of the core concepts of the DOM Event Model in JavaScript. It describes how events propagate (travel) through the DOM tree when an event is triggered.
When an event occurs on a nested element (like a button inside a div), it first runs the event handler on the target element, and then it "bubbles up" the DOM tree to its ancestors โ triggering any matching event handlers on parent elements, all the way up to the root.
Example:
<div id="parent">
<button id="child">Click Me</button>
</div>
document.getElementById('parent').addEventListener('click', () => {
console.log('Parent clicked');
});
document.getElementById('child').addEventListener('click', () => {
console.log('Child clicked');
});
Clicking the button logs:
Child clickedParent clickedYou can stop the bubbling using the event.stopPropagation() method:
document.getElementById('child').addEventListener('click', (e) => {
e.stopPropagation();
console.log('Child clicked (no bubbling)');
});
{ capture: true } as the third argument.stopPropagation() carefully, as it can break functionality if overused.