Events — *Responding to User Input*
Events — *Responding to User Input*
🎯 By the end of this lesson
After reading this lesson, you will be able to confidently do the following 3 things.
- ▸✅ Event capturing, bubbling, and delegation patterns
- ▸✅ Memory leaks from missing addEventListener cleanup
- ▸✅ When to apply debounce / throttle
Keep these learning goals as a checklist — once you can answer all of them, close the lesson.
The Event System
The Core in One Line
Events = signals that fire when the user does something. Clicks, key presses, scrolls, mouse movements, etc. JS receives these signals and acts.
10 Commonly Used Events
Event Listeners
> 💡 Inline onclick="..." is the old way. addEventListener is recommended — supports multiple handlers and removal.
The Event Object
Preventing Default Behavior
Event Propagation — Bubbling
Events propagate upward from child to parent. This is called bubbling.
Stopping propagation:
Event Delegation — An Efficient Pattern
1,000 listeners on each <li>? Inefficient. Instead, one listener on the parent:
Thanks to bubbling, the parent detects clicks on its children. Better memory and performance. Dynamically added children are handled automatically too.
debounce · throttle — Preventing Excessive Calls
In a search field, the API is called only once, 300ms after typing ends. 1,000 keystrokes → 1 API call.
Summary
- ▸
addEventListener('type', handler)is the standard - ▸
e.preventDefault()blocks default browser behavior - ▸
e.stopPropagation()stops bubbling - ▸Event delegation handles dynamic children efficiently
- ▸debounce · throttle optimize performance
⚡ Try It Yourself — Event System (mock EventTarget)
🤖 Try Asking AI This
Knowing the concepts in this lesson lets you give AI specific instructions. Instead of a vague 'fix this,' you can make vocabulary-driven requests — and that's where token savings begin.
- ▸"Apply debounce 300ms to this click event"
- ▸"Add cleanup to this event handler (return () => removeEventListener)"
Why This Saves Tokens
When you don't know the concepts, you have to follow up with 'What does that mean?' after every AI response. Those follow-up questions eat tokens. Learn the concept once, and the conversation finishes in one round.