Home JavaScript EventTarget JavaScript EventTarget Definition
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.
Syntax
JavaScript Syntax The EventTarget methods you will use every day:
function onSave(event) {
console.log(event.type);
}
button.addEventListener("click", onSave);
button.dispatchEvent(new Event("click"));
button.removeEventListener("click", onSave); Reference
Core Ideas Ideas every EventTarget tutorial builds on:
Idea Example Remember Listen el.addEventListener("click", fn)Can add many handlers Remove el.removeEventListener(...)Needs same function Dispatch el.dispatchEvent(event)Fire an event yourself Options { once: true }once, capture, passive Who has it Element, Document…Most DOM objects vs onclick el.onclick = fnOnly one onclick slot
Code
JavaScript Examples Three short programs that show how EventTarget works.
1. Listen 2. once 3. dispatch Example 1 — Add a click listener addEventListener registers a function for a named event.
const btn = document.createElement("button");
btn.addEventListener("click", function () {
console.log("clicked");
});
btn.click(); How It Works addEventListener stores the handler.btn.click() fires a click for testing.The handler prints clicked. Example 2 — Run a listener only once The once option removes the listener after the first call.
let n = 0;
btn.addEventListener("click", function () { n += 1; }, { once: true });
btn.click();
btn.click();
console.log(n); How It Works once: true means one run only.The first click() increments n. The second click does nothing; n stays 1. Example 3 — Dispatch a custom event Build an Event and send it through dispatchEvent.
el.addEventListener("save", function (event) {
console.log(event.type);
});
el.dispatchEvent(new Event("save")); How It Works addEventListener("save") listens for a custom name.new Event("save") creates the event object.dispatchEvent delivers it to the listeners.Continue
Next: EventTarget Guides Continue with the constructor and the three core methods.
Look up Why Constructor Create EventTarget objects addEventListener()Register a listener removeEventListener()Detach a listener dispatchEvent()Fire an event
Full lists: EventTarget constructor .
Frequently Asked Questions What is EventTarget? EventTarget is the interface that can hold event listeners. Elements, documents, and windows implement it so you can call addEventListener on them.
Why prefer addEventListener over onclick? onclick can hold only one function. addEventListener lets you attach many listeners and remove them individually.
Why did removeEventListener not work? The function reference must be the same one you passed to addEventListener. Inline anonymous functions are different each time.
What is dispatchEvent for? It fires an event on that target so listeners run, useful for custom events and tests.
Where should I go next? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion