JavaScript Event Delegation — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Event Delegation lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Delegate to a list
easy10 minPut one listener on the list and report the text of the item clicked.
- 2
Ignore clicks on the container
easy10 minReport nothing selected when the list itself is clicked.
- 3
Use closest for nested content
easy10 minThe click lands on a span, so walk up to the item and report its class.
- 4
Handle new elements
easy10 minAdd an item after attaching the listener and make it work when clicked.
- 5
Decide by data attribute
medium10 minReport which action the clicked button carries.
Find the bug(5)
- 1
Fix the listener on each item
easy8 minOne listener on the list handles every item, including new ones.
- 2
Fix currentTarget used to find the item
easy8 mincurrentTarget is the list, not the item that was clicked.
- 3
Fix the missing target check
easy8 minA click on the list itself should not be treated as an item.
- 4
Fix target used where closest is needed
easy8 minThe click lands on the span, so the item must be found by walking up.
- 5
Fix the delete removing the wrong element
easy8 minThe row should go, not just the button inside it.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace