Skip to content

Latest commit

 

History

History
36 lines (23 loc) · 1008 Bytes

README.md

File metadata and controls

36 lines (23 loc) · 1008 Bytes

30. Event delegation

Handle many related elements with one listener.

Learn

  • bubbling
  • closest
  • currentTarget
  • target

How it works

Events often bubble from a child to its ancestors. Delegation places one listener on a stable parent and uses closest to find the relevant child action. This works well for lists whose items are added later. Check that the matched element belongs to the intended container.

Common mistake

event.target may be an icon or span inside the button, not the button itself.

Guided challenge

Create a dynamic list with remove buttons and one listener on the list parent.

Run the example

Open example.html in a browser and edit example.js. Save, refresh, and use the Console and debugger to inspect values.

Completion check

  • Explain the core idea in your own words without reading the code.
  • Change one input and predict the output before running it.
  • Test one boundary or failure case, then record what you changed.