Skip to content

QuizNot attempted

Event delegation

You have a list of 1,000 rows that are added and removed often. Best way to handle clicks?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • One listener on the list container, then use event.target.closest("li") to find the row
  • Event delegation: clicks bubble, so one listener on the container catches all rows, event.target.closest('li') finds which row, and rows added later just work with no re-binding.
  • Not one listener per row: 1,000 listeners to attach, detach and leak on every change.
  • Not inline onclick: same per-row cost, plus global-scope handlers and CSP problems.
  • Not a MutationObserver: extra machinery to re-create the per-row approach.
  • Note focus/blur don't bubble (use focusin/focusout).