Skip to content
Justice Tech School

Event Delegation

Attach one listener to a parent and check event.target. It works for items added later and saves memory.

Example
const list = document.createElement("ul");
["One", "Two"].forEach((t) => { const li = document.createElement("li"); li.textContent = t; li.style.cursor = "pointer"; list.appendChild(li); });
list.addEventListener("click", (e) => { if (e.target.matches("li")) console.log("clicked", e.target.textContent); });
document.body.appendChild(list);

Exercise

Which element was clicked.

e.

Quiz Test

Why use event delegation?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.