Lesson 10 of 55
javascriptEvent Handling – Adding & Removing Listeners
13 mins
Course progress18%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 10 of 55
javascript13 mins
Keep these ideas in mind as you work through the lesson.
Event listeners are added with addEventListener.
button.addEventListener('click', handleClick, { once:true, passive:true });
type – event name (click, keydown).listener – function or object with a handleEvent method.options – capture, once, passive.Removing:
button.removeEventListener('click', handleClick);
document.body.addEventListener('click', e => {
if (e.target.matches('a[data-action]')) {
console.log('Link clicked:', e.target.dataset.action);
}
});