By the end of this lesson, learners will be able to:
event.target and event.currentTarget correctlyLet’s say you have a list of 100 items, and each should respond to a click.
If you do:
listItem1.addEventListener("click", ...);
listItem2.addEventListener("click", ...);
...
listItem100.addEventListener("click", ...);
✅ This works, but it is:
Event delegation solves these problems by using a single event listener on a common parent.
Event delegation means:
✅ This is faster, cleaner, and easier to maintain — especially with dynamic elements.
In the DOM, when an event happens (like a click), it first runs on the target element and then bubbles up through its ancestors (parent, grandparent, all the way to document).
That’s why if you put an event listener on the parent, it can “catch” events from its children.
| Property | Meaning |
|---|---|
event.target | The exact element that was clicked |
event.currentTarget | The element the event handler was attached to (the parent) |
✅ In event delegation, you almost always use event.target to check which child was clicked.
Let’s say you build a to-do list where the user can add new tasks:
HTML:
<ul id="task-list">
<li>First Task</li>
</ul>
<button id="add-task">Add Task</button>
JavaScript:
let list = document.getElementById("task-list");
let addBtn = document.getElementById("add-task");
addBtn.addEventListener("click", function() {
let li = document.createElement("li");
li.textContent = "New Task";
list.appendChild(li);
});
✅ If you put a click listener on each <li> you’d have to re-add the listener every time you create a new one.
Instead, use delegation on the parent:
list.addEventListener("click", function(event) {
if (event.target.tagName === "LI") {
event.target.classList.toggle("completed");
}
});
🧠 This means:
<ul>completed class updates the style.completed {
text-decoration: line-through;
color: gray;
}
Clicking any <li> will now mark it as completed — even if it was added later.
document unless needed)event.target inside the handler to see which element triggered the eventif (event.target.classList.contains("delete-btn")) {
// do delete logic
}
HTML:
<ul id="color-list">
<li>Red</li>
<li>Blue</li>
<li>Green</li>
</ul>
<button id="add-color">Add Color</button>
Task:
<li> with text "Yellow" each time the button is clicked<li> is clicked, toggle a highlight class✅ Example JavaScript:
let list = document.getElementById("color-list");
let button = document.getElementById("add-color");
button.addEventListener("click", function() {
let li = document.createElement("li");
li.textContent = "Yellow";
list.appendChild(li);
});
list.addEventListener("click", function(event) {
if (event.target.tagName === "LI") {
event.target.classList.toggle("highlight");
}
});
| Concept | Description |
|---|---|
| Event delegation | One listener on parent handles many children |
| Event bubbling | Events travel upward through the DOM tree |
| target | Element that was actually clicked |
| currentTarget | Element the handler is attached to |