Live Search, Forms & Event Delegation
Respond to real-time typing with the input event and handle clicks on dynamically created elements with event delegation.
What you'll learn
- Filter UI results live as the user types using the input event
- Use event delegation on a parent container instead of attaching dozens of listeners
- Read element metadata cleanly via data-* attributes and dataset
When your page has a list of 50 interactive items (or items added dynamically after page load)attaching a separate click listener to every button is messy. Event delegation solves this by putting one listener on the parent element and checking event.target.closest(...).
Try It Yourself
Type in the search box to filter tags in real time, and click any tag to select it via a single delegated listener:
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
1. What is the main advantage of event delegation on a parent container?
Finished this lesson?
Your progress is saved locally in your browser, no account needed.