Making pages interactive
An event is something that happens on the page — a click, a key press, a form submission, a mouse hover. Event listeners let you run code when these events occur.addEventListener, pass the event name and a function to run.
addEventListener()
You can attach multiple listeners to the same element and event. They all fire in the order they were added.
Common events
Practical examples
The event object
Every event handler receives anevent object with details about what happened:
Key properties
event.target — the most useful property
event.target tells you exactly which element was interacted with:
event.preventDefault() — stop default behavior
Some elements have built-in behaviors.preventDefault() stops them:
Event delegation
Instead of adding a listener to every list item, add one listener to the parent and checkevent.target:
- Works for elements added after the listener is set up (dynamically created elements)
- One listener instead of hundreds — better performance
- No need to re-attach listeners when the list changes
Removing event listeners
To remove a listener, you need a reference to the same function:One-time listeners
What’s next?
You can handle clicks and keyboard events. Let’s put it all together with the most common interactive element — forms.Form handling
Get form values, validate input, and handle submissions