Handle many related elements with one listener.
- bubbling
- closest
- currentTarget
- target
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.
event.target may be an icon or span inside the button, not the button itself.
Create a dynamic list with remove buttons and one listener on the list parent.
Open example.html in a browser and edit example.js. Save, refresh, and use the Console and debugger to inspect values.
- 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.