JavaScript EventTarget addEventListener() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

EventTarget.addEventListener(type, listener) registers a function (or handleEvent object) that runs when that event type is delivered to the target. Per MDN, it is the recommended way to listen for events — returns undefined.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call target.addEventListener. Start with this line.

JavaScript
target.addEventListener(type, listener, options?)
CallWhat it does
target.addEventListener(type, listener, options?)Primary signature.
target.addEventListener(...)Call with the arguments your case needs.

Built into the browser. Call addEventListener(...) on an EventTarget (or Element, Document, Window, …) — no import required.

Library declarations
JavaScript
target.addEventListener(type, listener, options?)

Parameters

ParameterDescription
typeRequired. Event name string (e.g. "click")
listenerRequired. Function or { handleEvent } object
options / useCaptureOptional. once, capture, passive, signal

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Basic addEventListener()

Register a click listener on a button.

JavaScript
const button = document.querySelector("#demoBtn");
button.addEventListener("click", function () {
  console.log("Button clicked!");
});
Try It Yourself

How It Works

  1. Select the target. Any EventTarget — here a button.
  2. Pass the type. "click" is case-sensitive.
  3. Pass a function. It runs when that event is delivered.
  4. No return value. addEventListener returns undefined.

Example 2 — Multiple Listeners

Attach more than one handler for the same event type.

JavaScript
const button = document.querySelector("#demoBtn");
button.addEventListener("click", () => console.log("first"));
button.addEventListener("click", () => console.log("second"));
// Click → logs both (unlike a single onclick property)
Try It Yourself

How It Works

  1. Register twice. Two different functions for "click".
  2. Both stay. MDN: unlike onclick = …, you can stack handlers.
  3. Click once. Both listeners run (order is registration order for the same phase).
  4. Libraries-friendly. Your code and a library can both listen safely.

Example 3 — { once: true }

Run the listener at most once, then remove it automatically.

JavaScript
const button = document.querySelector("#demoBtn");
let count = 0;
button.addEventListener("click", function () {
  count++;
  console.log("once fired, count=" + count);
}, { once: true });
// Second click does nothing
Try It Yourself

How It Works

  1. Pass options. Third argument is { once: true }.
  2. First click. Listener runs and is removed.
  3. Later clicks. No listener left — no more logs.
  4. Good for setup. One-shot init or “first interaction” handlers.

Example 4 — this Binding

In a regular function listener, this is the target element.

JavaScript
const button = document.querySelector("#demoBtn");
button.addEventListener("click", function (e) {
  console.log(this.id);                      // "demoBtn"
  console.log(e.currentTarget === this);     // true
});
// Arrow functions do NOT bind this to the element
Try It Yourself

How It Works

  1. Use a regular function. Not an arrow — arrows keep outer this.
  2. Read this. It equals the element that owns the listener.
  3. Match currentTarget. MDN: e.currentTarget === this.
  4. Prefer e.currentTarget. Clearer when you also use arrows or bound methods.

Example 5 — Remove with AbortSignal

Pass { signal }, then abort() to drop the listener.

JavaScript
const button = document.querySelector("#demoBtn");
const controller = new AbortController();

button.addEventListener("click", () => {
  console.log("still listening…");
}, { signal: controller.signal });

// Later:
controller.abort(); // listener is removed
Try It Yourself

How It Works

  1. Create a controller. new AbortController() owns a signal.
  2. Pass the signal. Third argument: { signal: controller.signal }.
  3. Abort when done. controller.abort() removes the listener.
  4. Handy cleanup. One abort can tear down many listeners that share the signal.

Browser Support

EventTarget.addEventListener() is marked Baseline Widely available on MDN (since July 2015). It is a browser Web API (also available in Web Workers). Some options such as signal have varying support in older browsers.

✓ Baseline · Widely available

EventTarget.addEventListener()

Supported across modern Chrome, Firefox, Safari, and Edge for registering event listeners.

98% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
addEventListener() Excellent

Bottom line: Safe for production. Prefer addEventListener over onXYZ properties; use removeEventListener or AbortSignal for cleanup.

Frequently Asked Questions

It registers a function (or an object with handleEvent) to run when the given event type is delivered to that target — for example a button click or a custom event on new EventTarget().
No. MDN marks EventTarget.addEventListener() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard. Some options (for example signal) have varying support across older browsers.
You can attach many handlers for the same event, choose capture vs bubble, use options like once and signal, and it works on any EventTarget — not only HTML elements.
The same function or handleEvent object is not added twice for the same target and type. Two separately created anonymous functions count as different listeners.
With { once: true }, the listener runs at most once and is then removed automatically.
Call removeEventListener with the same type and the same function reference, or pass { signal } from an AbortController and call abort().

Next: dispatchEvent()

Fire events on any EventTarget, including custom ones.

dispatchEvent() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful