Adding and Removing Event Listeners in JavaScript

6/22/2025

Visual showing JavaScript code flow with addEventListener and removeEventListener in a user interaction

Go Back

Adding and Removing Event Listeners in JavaScript: A Complete Guide

Event listeners are the foundation of interactive web applications. They allow JavaScript to respond to user actions such as clicks, key presses, mouse movements, and more. This article explains how to add and remove event listeners efficiently using addEventListener() and removeEventListener().

 Visual showing JavaScript code flow with addEventListener and removeEventListener in a user interaction

What is an Event Listener?

An event listener is a JavaScript function that waits for a specific event to occur on a particular element. When that event happens, the function is executed.

Adding Event Listeners

To add an event listener, use the addEventListener() method:

Syntax

element.addEventListener(event, handler, options);
  • event: The name of the event (e.g., click, keydown, mouseover).

  • handler: The function to be executed when the event occurs.

  • options: Optional parameter to specify settings like capture, once, and passive.

Example: Adding a Click Listener

const button = document.getElementById('myButton');

function handleClick() {
  console.log('Button clicked!');
}

button.addEventListener('click', handleClick);

Using Options

button.addEventListener('click', handleClick, { once: true });
// The handler will run once and then automatically be removed

Removing Event Listeners

To remove an event listener, use the removeEventListener() method. It's essential to pass the same function reference used in addEventListener().

Syntax

element.removeEventListener(event, handler, options);

Example: Removing a Click Listener

button.removeEventListener('click', handleClick);

Important Note:

Anonymous functions cannot be removed because removeEventListener needs a direct reference to the handler.

Incorrect Example:

// This won't work:
button.removeEventListener('click', function() {
  console.log('Clicked!');
});

Real-World Use Cases

1. Dynamic Interfaces

When building components that appear and disappear (like modals or tooltips), add listeners on show and remove them on hide to improve performance.

2. Performance Optimization

Removing unused listeners prevents memory leaks and unnecessary event processing.

3. Preventing Duplicate Handlers

If you attach a listener multiple times without removing the previous ones, the handler may fire more than once per event.

Best Practices

  • Always store references to handler functions if you plan to remove them later.

  • Use { once: true } when you only need the handler to run once.

  • Clean up event listeners when removing elements from the DOM to avoid memory leaks.

Conclusion

Mastering the use of addEventListener() and removeEventListener() is crucial for creating dynamic, responsive, and performant JavaScript applications. Whether you’re building a simple form or a complex web app, properly managing your event listeners ensures a better user experience and cleaner code.

Table of content