User interaction in web applications is powered by the JavaScript event system. Event listeners observe events dispatched by the browser (clicks, keypresses, network states, form submissions) and execute designated callback functions in response.
1. The addEventListener Architecture
The standard method for attaching handlers is EventTarget.prototype.addEventListener():
addEventListener() allows multiple listeners for the same event type, supports passive options, and maintains a strict separation of presentation and logic.
2. The Event Object: type, target, and currentTarget
Whenever an event triggers, the browser automatically passes an Event object to the callback:
JavaScript
1button.addEventListener('click',(e)=>{
2 console.log('Event Type:', e.type);// 'click'
3 console.log('Origin Element (target):', e.target);// Deepest element that was clicked
4 console.log('Bound Element (currentTarget):', e.currentTarget);// Element hosting this listener
5 console.log('Timestamp:', e.timeStamp);
6});
target vs. currentTarget
event.target: The exact element that initiated the event (e.g., an <i> icon inside a <button>).
event.currentTarget: The element to which the event listener is currently attached (the <button>).
3. Removing Event Listeners with removeEventListener()
To remove an event listener, you must supply the identical function reference. Anonymous inline arrow functions cannot be removed!
13// window.removeEventListener('resize', () => console.log('Resized')); // FAILS: Different memory reference!
4. Advanced Event Options: once, passive, and capture
addEventListener accepts a third argument—either a boolean (for capture) or an options object:
JavaScript
1button.addEventListener('click',()=>{
2 console.log('This will only run once!');
3},{once:true});// Automatically detaches after firing once
4
5// Performance optimization for scroll/touch events
6window.addEventListener('scroll', handleScroll,{
7passive:true// Informs browser that preventDefault() will NEVER be called, preventing UI lag
8});
5. AbortController: Modern Event Cleanup
ES2022 introduced signal-based event cancellation using AbortController. This allows you to remove dozens of event listeners simultaneously with a single command:
JavaScript
1const controller =newAbortController();
2const{ signal }= controller;
3
4const btn1 = document.querySelector('#b1');
5const btn2 = document.querySelector('#b2');
6
7btn1.addEventListener('click',()=> console.log('Btn 1 clicked'),{ signal });
8btn2.addEventListener('click',()=> console.log('Btn 2 clicked'),{ signal });
9
10// Later, cancel ALL listeners attached with this signal
11controller.abort();
Interactive Quiz• Knowledge Check
Practice Quiz
Question 1
Why can an anonymous arrow function listener NOT be removed via removeEventListener()?
Question 2
What is the difference between event.target and event.currentTarget?
Question 3
What listener option ensures that an event handler fires only a single time and then auto-detaches?
Question 4
What does setting { passive: true } achieve on touch and scroll event listeners?
Question 5
Which modern JavaScript API allows removing multiple event listeners across various elements using a single signal?
Next Lesson
Event Bubbling & Capturing
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.