Event Propogation in js
Event Propagation in JavaScript
Understanding How Events Travel Through the DOM
Imagine you're building a website with a navigation menu.
Inside the menu, you have a list of items. Each item contains a button, and that button contains an icon.
Now imagine a user clicks that icon.
Which element receives the click event?
Is it the icon? The button? The list item? Or the entire navigation menu?
The interesting part is that all these elements can participate in handling the same click event.
This happens because of a mechanism called Event Propagation.
Before understanding it, let's look at how HTML elements are organized.
1. Understanding the DOM Hierarchy
When a browser reads HTML, it creates a tree-like structure called the Document Object Model (DOM).
Consider this HTML:
<div>
<form>
<button>
Click Me
</button>
</form>
</div>
The browser represents it as a hierarchy:
DIV
|
FORM
|
BUTTON
Here:
divis the parent ofform.formis the parent ofbutton.buttonis the child ofform.
Now imagine the user clicks the button.
The browser doesn't treat the click as an isolated event that belongs only to the button.
Instead, the event travels through the DOM hierarchy.
This journey is called Event Propagation.
2. The Three Phases of Event Propagation
Event propagation consists of three phases:
Capturing Phase
Target Phase
Bubbling Phase
Let's understand each one.
The Deep-Sea Submarine Analogy
Imagine a deep-sea research submarine descending toward a rare seashell at the bottom of the ocean.
The submarine travels through different levels before reaching its destination.
Capturing: The submarine descends from the ocean surface toward the seabed.
Target: The submarine reaches the seashell.
Bubbling: The submarine sends a signal that travels back toward the surface.
Event propagation follows a similar journey.
An event travels down through the DOM hierarchy, reaches the target element, and then travels back up through its ancestors.

Phase 1: Capturing Phase
The capturing phase is the first phase of event propagation.
When an event occurs, the browser travels from the outermost relevant ancestor toward the target element.
For example, if the user clicks a button inside a form and div:
Window
↓
Document
↓
HTML
↓
BODY
↓
DIV
↓
FORM
↓
BUTTON
By default, event listeners don't execute during the capturing phase.
To listen during capturing, we need to pass { capture: true } to addEventListener().
const div = document.querySelector("div");
div.addEventListener(
"click",
function () {
console.log("DIV");
},
{ capture: true }
);
Now the listener is registered for the capturing phase.
Phase 2: Target Phase
The second phase is the target phase.
This is where the event reaches the actual element on which the interaction occurred.
For example, if the user clicks the button:
const button = document.querySelector("button");
button.addEventListener("click", function () {
console.log("BUTTON");
});
The button is the target element.
The event has completed its downward journey and reached its destination.
An important detail: listeners registered on the target can run during both the capturing and bubbling listener-processing stages. The target itself is not skipped simply because a listener uses capture: true.
Phase 3: Bubbling Phase
After reaching the target, the event travels back up through the DOM hierarchy.
For example:
BUTTON
↑
FORM
↑
DIV
↑
BODY
↑
HTML
↑
DOCUMENT
↑
WINDOW
This is called the bubbling phase.
By default, when we use addEventListener(), the listener is registered for the bubbling phase.
For example:
const div = document.querySelector("div");
const form = document.querySelector("form");
const button = document.querySelector("button");
div.addEventListener("click", () => {
console.log("DIV");
});
form.addEventListener("click", () => {
console.log("FORM");
});
button.addEventListener("click", () => {
console.log("BUTTON");
});
Suppose the HTML is:
<div>
<form>
<button>Click Me</button>
</form>
</div>
When we click the button, the output is:
BUTTON
FORM
DIV
Why?
Because the event first reaches the button and then bubbles upward through its parent elements.
Remember: Capturing travels from parent to child, while bubbling travels from child to parent.
3. Understanding Event Capturing with Code
Let's use the same DOM structure:
<div>
<form>
<button>Click Me</button>
</form>
</div>
Now register all three listeners using capturing:
const div = document.querySelector("div");
const form = document.querySelector("form");
const button = document.querySelector("button");
div.addEventListener("click", function () {
console.log("DIV");
}, { capture: true });
form.addEventListener("click", function () {
console.log("FORM");
}, { capture: true });
button.addEventListener("click", function () {
console.log("BUTTON");
}, { capture: true });
When we click the button, the output is:
DIV
FORM
BUTTON
Why?
Because the event is travelling downward during the capturing phase.
Now what if we register both capturing and bubbling listeners?
div.addEventListener("click", () => {
console.log("DIV CAPTURE");
}, true);
form.addEventListener("click", () => {
console.log("FORM CAPTURE");
}, true);
button.addEventListener("click", () => {
console.log("BUTTON CAPTURE");
}, true);
button.addEventListener("click", () => {
console.log("BUTTON BUBBLE");
});
form.addEventListener("click", () => {
console.log("FORM BUBBLE");
});
div.addEventListener("click", () => {
console.log("DIV BUBBLE");
});
Output:
DIV CAPTURE
FORM CAPTURE
BUTTON CAPTURE
BUTTON BUBBLE
FORM BUBBLE
DIV BUBBLE
This demonstrates the complete propagation journey.
First, the event travels downward. Then it reaches the target, where both target listeners run. Finally, it bubbles upward.
4. Event Target vs CurrentTarget
This is one of the most frequently asked JavaScript interview questions.
Let's understand the difference.
Consider this HTML:
<div>
<form>
<button>
<span>Click Me</span>
</button>
</form>
</div>
Now attach listeners:
const div = document.querySelector("div");
const form = document.querySelector("form");
const button = document.querySelector("button");
function handleClick(event) {
console.log("Target:", event.target.tagName);
console.log("Current Target:", event.currentTarget.tagName);
console.log("This:", this.tagName);
}
div.addEventListener("click", handleClick);
form.addEventListener("click", handleClick);
button.addEventListener("click", handleClick);
Suppose the user clicks the span inside the button.
The output will be:
Target: SPAN
Current Target: BUTTON
This: BUTTON
Target: SPAN
Current Target: FORM
This: FORM
Target: SPAN
Current Target: DIV
This: DIV
Why?
Because these properties represent different things.
event.target
event.target identifies the original event target: the deepest DOM element on which the event originated.
In our example:
event.target.tagName
Returns:
SPAN
The span was clicked, so it remains the target while the event propagates.
event.currentTarget
event.currentTarget identifies the element whose event listener is currently executing.
For example:
When the button listener runs,
currentTargetis the button.When the form listener runs,
currentTargetis the form.When the div listener runs,
currentTargetis the div.
What about this?
For a normal function used as an event listener, this generally refers to the element on which the listener was registered.
So, in this example, this and event.currentTarget refer to the same element.
However, arrow functions behave differently because they don't have their own this.
Quick comparison
Property | Meaning |
|---|---|
| Original event target |
| Element whose listener is currently executing |
| Usually the listener element for a normal function callback |
Interview tip: target remains associated with the original event target, while currentTarget changes as different listeners execute.
5. Stopping Event Propagation
Sometimes we don't want an event to continue travelling through the DOM.
JavaScript provides methods to control event propagation.
There are two important methods:
stopPropagation()stopImmediatePropagation()
stopPropagation()
This method prevents the event from continuing to propagate to other elements in the propagation path.
Consider:
const div = document.querySelector("div");
const form = document.querySelector("form");
const button = document.querySelector("button");
div.addEventListener("click", () => {
console.log("DIV");
});
form.addEventListener("click", (event) => {
console.log("FORM");
event.stopPropagation();
});
button.addEventListener("click", () => {
console.log("BUTTON");
});
When we click the button, the output is:
BUTTON
FORM
Why isn't DIV printed?
Because when the event reaches the form, stopPropagation() prevents it from continuing to the div.
Notice that the button and form listeners still execute. The method doesn't undo listeners that have already run.
stopImmediatePropagation()
Now imagine we have multiple listeners attached to the same element.
const button = document.querySelector("button");
button.addEventListener("click", (event) => {
console.log("First Listener");
event.stopImmediatePropagation();
});
button.addEventListener("click", () => {
console.log("Second Listener");
});
Output:
First Listener
Why?
Because stopImmediatePropagation() does two things:
Stops further propagation through the DOM.
Prevents other listeners on the same element from executing.
Difference between the two
|
|
|---|---|
Stops propagation to other elements | Stops propagation to other elements |
Doesn't stop other listeners on the same element | Also prevents later listeners on the same element from running |
Used to stop the event's journey through the DOM | Used when both propagation and later same-element listeners must stop |
Important: What these methods don't do
Neither method prevents the browser's default action.
For example, clicking a link may still navigate even if propagation is stopped.
To prevent the default browser behavior, use:
event.preventDefault();
These methods solve different problems:
stopPropagation()controls event propagation.preventDefault()prevents the browser's default action.
Avoid using propagation-stopping methods unnecessarily. They can interfere with other application logic, analytics listeners, and shared event handlers.
6. Event Delegation
Now let's understand one of the most useful patterns built on event bubbling.
Imagine an e-commerce website displaying 1,000 products.
Each product has a delete button.
A straightforward approach would be to attach a click listener to every delete button.
const buttons = document.querySelectorAll(".delete");
buttons.forEach((button) => {
button.addEventListener("click", () => {
console.log("Product deleted");
});
});
This works, but what happens when new products are added dynamically?
We would need to attach listeners to those new buttons as well.
Managing listeners across dynamically created elements can become inconvenient.
Wouldn't it be easier if we could attach just one listener to the parent container and handle clicks from all its children?
That's exactly what Event Delegation allows us to do.
What is Event Delegation?
Event delegation is a technique where we attach a single event listener to a parent element and use event propagation to handle events originating from its child elements.
Instead of attaching 1,000 listeners to 1,000 buttons, we can attach one listener to their common parent.
Let's see how.
Without Event Delegation
<ul class="products">
<li>
<button class="delete">Delete Product 1</button>
</li>
<li>
<button class="delete">Delete Product 2</button>
</li>
<li>
<button class="delete">Delete Product 3</button>
</li>
</ul>
We attach listeners individually:
document.querySelectorAll(".delete").forEach((button) => {
button.addEventListener("click", () => {
console.log("Product deleted");
});
});
With Event Delegation
Instead of attaching listeners to individual buttons, attach one listener to the parent:
const products = document.querySelector(".products");
products.addEventListener("click", (event) => {
if (event.target.classList.contains("delete")) {
console.log("Product deleted");
}
});
What happens?
The user clicks a delete button.
The click event reaches that button.
The event bubbles up to the products container.
The container's listener executes.
We inspect
event.targetto identify the clicked element.If it is a delete button, we perform the required operation.
Now the same listener can handle existing and newly added buttons, as long as the new buttons are inside the delegated container and the event bubbles.

Handling Nested Elements with closest()
There is one important problem with the previous example.
What if our button contains an icon or span?
<ul class="products">
<li>
<button class="delete" data-id="42">
<span>Delete</span>
</button>
</li>
</ul>
If the user clicks the span, then:
event.target
Will refer to the span, not the button.
Therefore, this check will fail:
event.target.classList.contains("delete");
Because the span doesn't have the delete class.
We can solve this using closest().
const products = document.querySelector(".products");
products.addEventListener("click", (event) => {
const button = event.target.closest("button.delete");
if (!button || !products.contains(button)) {
return;
}
console.log("Delete product:", button.dataset.id);
});
Now it works even when the user clicks an element nested inside the button.
closest() searches the element itself and its ancestors until it finds a matching selector.
The additional contains() check ensures that the matched button belongs to our delegated container.
Why is Event Delegation useful?
Fewer event listeners: We can handle many child elements using one listener.
Dynamic elements: Newly added children can be handled without attaching new listeners individually.
Centralized logic: Event handling can be managed from a common parent.
Simpler maintenance: We avoid repeating the same event listener logic across many elements.
Event delegation is especially useful for lists, tables, menus, product grids, and dynamically generated UI.
7. Practical Example: Closing a Modal by Clicking Outside
Let's apply what we've learned to a common UI requirement.
Imagine a modal dialog.
We want it to close when the user clicks the empty background outside the modal, but not when they click inside the modal content.
HTML
<div class="modalContainer">
<div class="modal">
<h2>Welcome to Consistcode</h2>
<p>This is a modal dialog.</p>
<button class="modalButton">Close</button>
</div>
</div>
CSS
.modalContainer {
display: none;
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
justify-content: center;
align-items: center;
}
.modal {
background: white;
padding: 24px;
border-radius: 8px;
}
JavaScript
const container = document.querySelector(".modalContainer");
const button = document.querySelector(".modalButton");
function toggleModal(show) {
container.style.display = show ? "flex" : "none";
}
toggleModal(true);
button.addEventListener("click", () => {
toggleModal(false);
});
container.addEventListener("click", (event) => {
if (event.target === container) {
toggleModal(false);
}
});
Let's understand the important condition:
if (event.target === container)
When the user clicks the empty background, the event target is the container itself.
But when the user clicks inside the modal, the event target is the modal or one of its children.
Therefore, the modal closes only when the background is clicked.
This is a simple example of using event propagation and event targets to control UI behavior.
8. Events That Don't Bubble
An important detail: not every DOM event bubbles.
Some commonly known examples include:
focusblurmouseentermouseleave
For example:
const parent = document.querySelector(".parent");
parent.addEventListener("focus", () => {
console.log("Parent focused");
});
A normal focus event on a descendant doesn't bubble to the parent.
If we want to handle focus events through a parent, we can use focusin, which does bubble.
Similarly:
Non-bubbling event | Bubbling alternative |
|---|---|
|
|
|
|
|
|
|
|
Be careful: the alternatives aren't perfectly identical in behavior. For example, mouseenter and mouseleave don't behave exactly like mouseover and mouseout when moving across nested elements.
Also, some events can be captured even when they don't bubble, so capturing and bubbling are separate concepts.
9. Common Pitfalls and Interview Traps
Mistake 1: Confusing target and currentTarget
element.addEventListener("click", (event) => {
console.log(event.target);
console.log(event.currentTarget);
});
target identifies where the event originated.
currentTarget identifies the element whose listener is currently running.
Mistake 2: Assuming every event bubbles
Not every DOM event bubbles.
For example, focus and blur don't bubble. Use focusin and focusout when bubbling behavior is needed.
Mistake 3: Overusing stopPropagation()
Stopping propagation can interfere with other listeners and shared application behavior.
Use it only when you genuinely need to stop an event from reaching other elements in the propagation path.
Mistake 4: Forgetting nested elements in event delegation
A user might click a span or icon inside a button.
Checking only event.target can fail.
Use:
event.target.closest(".delete");
Then ensure the matched element belongs to the delegated container.
Mistake 5: Confusing preventDefault() and stopPropagation()
They are not interchangeable.
event.preventDefault();
Prevents the browser's default action.
event.stopPropagation();
Stops the event from continuing through the propagation path.
10. Interview Questions and Output-Based Problems
Q1. What are the three phases of event propagation?
Capturing: The event travels from outer ancestors toward the target.
Target: The event reaches the target element.
Bubbling: The event travels from the target back through its ancestors.
Q2. What is the default event listener phase?
The default phase for addEventListener() is bubbling.
To listen during capturing, pass { capture: true }.
Q3. What is the difference between event.target and event.currentTarget?
event.target identifies the original event target, while event.currentTarget identifies the element whose listener is currently executing.
Q4. What is event delegation?
Event delegation is a technique in which a parent element handles events from its descendants using a single event listener.
It relies on event propagation, usually bubbling.
Q5. What is the difference between stopPropagation() and stopImmediatePropagation()?
stopPropagation() stops the event from continuing through the DOM propagation path.
stopImmediatePropagation() also prevents subsequent listeners on the same element from executing.
Q6. Does preventDefault() stop event propagation?
No. It prevents the browser's default action but doesn't stop the event from travelling through the DOM.
Q7. Can we use event delegation for dynamically created elements?
Yes. As long as the elements are descendants of the delegated parent and the relevant event bubbles, the parent's listener can handle them without attaching new listeners.
Q8. Do all DOM events bubble?
No. Events such as focus, blur, mouseenter, and mouseleave don't bubble.
Q9. Can capturing and bubbling listeners both execute on the target?
Yes. Listeners registered on the target can execute in both the capturing-listener and bubbling-listener processing stages. The target is a special part of the event dispatch process.
Q10. Why is event delegation useful?
It reduces the need for individual listeners, supports dynamically created child elements, and centralizes event handling.
11. Quick Revision Cheat Sheet
Concept | Meaning |
|---|---|
Event Propagation | The journey of an event through the DOM |
Capturing | Event travels from ancestors toward the target |
Target | The element where the event originated |
Bubbling | Event travels from the target toward ancestors |
| Original event target |
| Element whose listener is currently executing |
| Stops further propagation |
| Stops propagation and later listeners on the same element |
| Prevents the browser's default action |
Event Delegation | One parent listener handles descendant events |
| Finds the nearest matching element or ancestor |
The most important things to remember
Event propagation has three phases: capturing, target, and bubbling.
Capturing travels downward; bubbling travels upward.
Event listeners use bubbling by default.
event.targetandevent.currentTargetare different.stopPropagation()andpreventDefault()solve different problems.Event delegation uses bubbling to handle child events through a parent.
Use
closest()when events may originate from nested elements.Not all DOM events bubble.
What's Next?
We now understand how events travel through the DOM and how we can use bubbling to handle events efficiently through event delegation.
But event propagation is only one part of browser event handling.
In the next chapter, we'll explore the JavaScript Event Loop, where we'll understand how JavaScript executes synchronous code, handles asynchronous callbacks, and manages the call stack, task queue, microtask queue, and event loop.
This will connect our understanding of Promises, async/await, and browser events into one bigger picture.