JavaScript Curriculum
Events — Fundamentals
easyThe coffee shop page is live. Now customers need to interact — clicking the order button, pressing Enter to search, submitting the contact form. Events are how JavaScript listens and responds.
Events — Fundamentals
The browser fires events constantly — clicks, keypresses, form submissions, scroll, resize. addEventListener is how you listen for them and run code in response.
addEventListener vs onclick vs HTML attributes
Three ways to attach events — only one is correct for modern code:
The modern standard. Attach multiple listeners to the same event. Can be removed later. Always use this.
const btn = document.querySelector('.order-btn')
btn.addEventListener('click', (event) => {
console.log('Clicked!', event.target)
})
// Multiple listeners on same event — both fire:
btn.addEventListener('click', () => logAnalytics('order-click'))// Click the demo button
The Event Object
Every listener receives an event object packed with information about what happened:
event.targetThe element that triggered the event — the one actually clicked.
<button class="order-btn">btn.addEventListener('click', (event) => {
console.log(event.target)
// → <button class="order-btn">
})Removing Event Listeners
Listeners can be removed — essential for cleanup in single-page apps:
addEventListener('click', handler)always firesaddEventListener('click', handler, { once: true })fires onceremoveEventListener('click', namedFn)attachedfunction namedFn() { console.log('fired') }
btn.addEventListener('click', namedFn)
// later:
btn.removeEventListener('click', namedFn)
// Must pass same function reference!// Click buttons to see listeners fire
Your Challenge
Attach two click listeners to the order button — one logs event.type, one logs event.timeStamp. Both should fire on each click. Then attach a third with { once: true } that logs "first click bonus!" — verify it only fires once.
Challenge
Attach a click listener to the order button that logs event.target.textContent. Attach a second listener to the same button that logs the timestamp. Verify both fire. Then use { once: true } to attach a third listener that fires only once.
Events — Fundamentals
easyThe coffee shop page is live. Now customers need to interact — clicking the order button, pressing Enter to search, submitting the contact form. Events are how JavaScript listens and responds.
Events — Fundamentals
The browser fires events constantly — clicks, keypresses, form submissions, scroll, resize. addEventListener is how you listen for them and run code in response.
addEventListener vs onclick vs HTML attributes
Three ways to attach events — only one is correct for modern code:
The modern standard. Attach multiple listeners to the same event. Can be removed later. Always use this.
const btn = document.querySelector('.order-btn')
btn.addEventListener('click', (event) => {
console.log('Clicked!', event.target)
})
// Multiple listeners on same event — both fire:
btn.addEventListener('click', () => logAnalytics('order-click'))// Click the demo button
The Event Object
Every listener receives an event object packed with information about what happened:
event.targetThe element that triggered the event — the one actually clicked.
<button class="order-btn">btn.addEventListener('click', (event) => {
console.log(event.target)
// → <button class="order-btn">
})Removing Event Listeners
Listeners can be removed — essential for cleanup in single-page apps:
addEventListener('click', handler)always firesaddEventListener('click', handler, { once: true })fires onceremoveEventListener('click', namedFn)attachedfunction namedFn() { console.log('fired') }
btn.addEventListener('click', namedFn)
// later:
btn.removeEventListener('click', namedFn)
// Must pass same function reference!// Click buttons to see listeners fire
Your Challenge
Attach two click listeners to the order button — one logs event.type, one logs event.timeStamp. Both should fire on each click. Then attach a third with { once: true } that logs "first click bonus!" — verify it only fires once.
Challenge
Attach a click listener to the order button that logs event.target.textContent. Attach a second listener to the same button that logs the timestamp. Verify both fire. Then use { once: true } to attach a third listener that fires only once.