JavaScript EventTarget

Beginner
8 min read
Updated: Nov 2026
3 examples
Listeners

Definition and Usage

EventTarget is the interface that can receive events and store listeners. Element, Document, and Window all implement it.

The three core methods are addEventListener, removeEventListener, and dispatchEvent. Prefer addEventListener over onclick = ... so you can attach more than one listener.

This page teaches what EventTarget is. Constructor and method guides live under this section.

Same function reference to remove.

Store the handler in a variable if you plan to call removeEventListener later. Anonymous functions cannot be removed.

JavaScript Syntax

The EventTarget methods you will use every day:

JavaScript
function onSave(event) {
  console.log(event.type);
}
button.addEventListener("click", onSave);
button.dispatchEvent(new Event("click"));
button.removeEventListener("click", onSave);

Core Ideas

Ideas every EventTarget tutorial builds on:

IdeaExampleRemember
Listenel.addEventListener("click", fn)Can add many handlers
Removeel.removeEventListener(...)Needs same function
Dispatchel.dispatchEvent(event)Fire an event yourself
Options{ once: true }once, capture, passive
Who has itElement, Document…Most DOM objects
vs onclickel.onclick = fnOnly one onclick slot

JavaScript Examples

Three short programs that show how EventTarget works.

Example 1 — Add a click listener

addEventListener registers a function for a named event.

JavaScript
const btn = document.createElement("button");
btn.addEventListener("click", function () {
  console.log("clicked");
});
btn.click();

How It Works

  1. addEventListener stores the handler.
  2. btn.click() fires a click for testing.
  3. The handler prints clicked.

Example 2 — Run a listener only once

The once option removes the listener after the first call.

JavaScript
let n = 0;
btn.addEventListener("click", function () { n += 1; }, { once: true });
btn.click();
btn.click();
console.log(n);

How It Works

  1. once: true means one run only.
  2. The first click() increments n.
  3. The second click does nothing; n stays 1.

Example 3 — Dispatch a custom event

Build an Event and send it through dispatchEvent.

JavaScript
el.addEventListener("save", function (event) {
  console.log(event.type);
});
el.dispatchEvent(new Event("save"));

How It Works

  1. addEventListener("save") listens for a custom name.
  2. new Event("save") creates the event object.
  3. dispatchEvent delivers it to the listeners.

Next: EventTarget Guides

Continue with the constructor and the three core methods.

Look upWhy
ConstructorCreate EventTarget objects
addEventListener()Register a listener
removeEventListener()Detach a listener
dispatchEvent()Fire an event

Full lists: EventTarget constructor.

Frequently Asked Questions

EventTarget is the interface that can hold event listeners. Elements, documents, and windows implement it so you can call addEventListener on them.
onclick can hold only one function. addEventListener lets you attach many listeners and remove them individually.
The function reference must be the same one you passed to addEventListener. Inline anonymous functions are different each time.
It fires an event on that target so listeners run, useful for custom events and tests.
Open the EventTarget constructor, then Event and MouseEvent for the event objects listeners receive.

Open the EventTarget constructor

Then practice addEventListener, removeEventListener, and dispatchEvent.

EventTarget constructor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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