Skip to the page

The DOM and events · core

Event delegation

Delegation puts one listener on a parent and uses event.target.closest() to find which child was hit, so it works for many items and for items added later.

The notes

What you must know

  • One listener for 1,000 rows instead of 1,000 listeners: less memory, works for new rows.
  • Find the item with const row = e.target.closest('[data-id]'); if (!row || !list.contains(row)) return;.
  • Read data from data-* attributes (row.dataset.id).
  • Doesn't work directly for non-bubbling events; use focusin, or capture.

The red pen

Common traps: mistake → fix

  • Mistake: "e.target is always the row"

    Fix: it is the innermost element clicked (an icon or span), so use closest().

Easy to hard

Questions interviewers ask, easy to hard

  1. What is event delegation and why use it?
  2. How do you handle a click on an icon inside the row?
  3. When doesn't delegation work?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview