Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท DOM: Events

EventTarget.removeEventListener() method

Reference for removeEventListener(): detach an event handler, match the same function and capture flag, and use once or AbortController as easier alternatives.

The removeEventListener() method detaches a listener that was added earlier with addEventListener(). After it is removed, the function is no longer called when the event fires.

The browser looks for a registration that matches on three things: the event type, the listener function, and the capture flag. All three must be equal to what you used when adding it. If nothing matches, the call quietly does nothing.

JavaScript
target.removeEventListener(type, listener);
target.removeEventListener(type, listener, options);

Try it

Stop listening after three clicks
Why an inline function cannot be removed

Syntax

JavaScript
removeEventListener(type, listener)
removeEventListener(type, listener, options)
removeEventListener(type, listener, useCapture)

Parameters

Name Description
type The event name used when the listener was added, for example "click".
listener The exact function (or handleEvent object) that was registered.
options Optional object. Only capture matters here; other properties such as once and passive are ignored.
useCapture Optional boolean, the older form of { capture }. Defaults to false.

Return value

undefined.

Usage notes

  • Same reference required. el.removeEventListener("click", () => {}) never removes anything, because the new arrow function is a different object. Store the function in a variable or use a named function.
  • Match the capture flag. A listener added with { capture: true } is removed only by a call with capture: true. A mismatch is a frequent reason why "removeEventListener does not work".
  • Bound functions are new functions. fn.bind(this) returns a new function each time. Save the bound result once and use that same reference for both add and remove.
  • Safe to call when nothing matches. It never throws for an unknown listener, so you can call it defensively during cleanup.
  • Removing inside the listener. A listener may remove itself while it runs. The current call finishes normally.
  • Easier alternatives. The once: true option removes the listener after its first call, and a signal from an AbortController removes any number of listeners with one abort().
  • Memory. Listeners keep their closures alive. Remove them when you delete long-lived targets such as window or document listeners from a component that goes away.

Browser support

removeEventListener() works in every browser. Passing an options object as the third argument is also widely supported; check a compatibility table if you need very old browsers.