11. Events & Interactivity
Events
Without Events, a webpage is just a static poster. Events are "things that happen" — a mouse click, a keyboard press, a form submission. JavaScript can listen for these events and execute code when they occur.
🎧 Event Listeners
You attach an "Event Listener" to a DOM element. It waits silently in the background until the event fires.
⌨️ The Event Object
When an event fires, the browser passes an "Event Object" (usually named e or event) into your function. It contains critical data about what just happened.
🫧 Event Bubbling & Delegation
When you click a button inside a <div>, the click triggers on the button, but then "bubbles up" and triggers on the <div> too. This allows a senior architecture pattern called Event Delegation.
Instead of adding 100 event listeners to a list of 100 items, you add one listener to the parent container, and check what was clicked.
Knowledge Check
Ready to test your understanding of 11. Events & Interactivity?