JavaScript EventTarget dispatchEvent() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Instance method

What You’ll Learn

dispatchEvent() is how you fire an event on any EventTarget. Create an Event or CustomEvent, then dispatch it to run listeners registered with addEventListener(). Learn the synchronous return value, preventDefault, isTrusted, five examples, and try-it labs.

01

Kind

Instance method

02

Input

Event object

03

Returns

true / false

04

Timing

Synchronous

05

isTrusted

false

06

Workers

Supported

Introduction

Listening alone is not enough — something must deliver the event. Native clicks are fired by the browser. Your own app events are fired with dispatchEvent().

First create the event (new Event("ping") or new CustomEvent("change", { detail })), then call target.dispatchEvent(event). Matching listeners run right away, before the next line of your code.

💡
Last step to fire

MDN: calling dispatchEvent() is the last step. The event must already be created and initialized with a constructor.

Pair this page with addEventListener() and the EventTarget constructor.

Understanding the dispatchEvent() Method

dispatchEvent sends the event through the normal processing rules (including capture and bubble on DOM nodes). For a plain new EventTarget(), listeners on that object simply run in order.

  • Event.target becomes the object you called dispatchEvent on.
  • Handlers run on a nested call stack — they finish before dispatchEvent returns.
  • Exceptions in handlers are reported as uncaught; they do not bubble to your caller.
  • Synthetic events have isTrusted === false.

📝 Syntax

General form of EventTarget.dispatchEvent:

JavaScript
dispatchEvent(event)

Parameters

  • event — the Event (or CustomEvent) to dispatch. Its target will be set to this EventTarget.

Return value

  • false — the event is cancelable and a listener called preventDefault().
  • true — otherwise (including non-cancelable events).

Exceptions

TypeError if the event type was not specified during initialization.

Common patterns

JavaScript
const target = new EventTarget();

target.addEventListener("ping", () => console.log("pong"));
target.dispatchEvent(new Event("ping"));

// With data:
target.dispatchEvent(
  new CustomEvent("update", { detail: { count: 3 } })
);

// Cancelable + preventDefault → false
const ok = target.dispatchEvent(
  new Event("save", { cancelable: true })
);

⚡ Quick Reference

GoalCode
Fire a simple eventtarget.dispatchEvent(new Event("ping"))
Fire with datanew CustomEvent("x", { detail: value })
Allow cancelnew Event("x", { cancelable: true })
Check if canceledconst ok = target.dispatchEvent(evt)
Synthetic?event.isTrusted === false
Listen firsttarget.addEventListener("ping", fn)

🔍 At a Glance

Four facts to remember about dispatchEvent().

Create first
new Event()

Then dispatch

Timing
sync

Listeners finish first

Returns
boolean

false if prevented

isTrusted
false

Synthetic events

📋 Native browser events vs dispatchEvent()

Native (click, keydown…)dispatchEvent()
Who fires it?Browser / user actionYour code
SchedulingUsually queued on the event loopSynchronous call
isTrustedtruefalse
Return valueN/A to your scripttrue / false
Best forReal UI inputApp / custom events

Examples Gallery

Examples follow MDN EventTarget.dispatchEvent() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Create an event and dispatch it to a target.

Example 1 — Basic dispatchEvent()

Listen, then fire a simple named event.

JavaScript
const target = new EventTarget();

target.addEventListener("hello", () => {
  console.log("Hello from dispatchEvent");
});

target.dispatchEvent(new Event("hello"));
// Hello from dispatchEvent
Try It Yourself

How It Works

The type string on new Event("hello") must match the listener type. No DOM element is required.

Example 2 — Payload with CustomEvent

Pass data through detail when you dispatch.

JavaScript
const bus = new EventTarget();

bus.addEventListener("user:login", (e) => {
  console.log(e.detail.name, e.detail.id);
});

bus.dispatchEvent(
  new CustomEvent("user:login", {
    detail: { name: "Ada", id: 42 }
  })
);
// Ada 42
Try It Yourself

How It Works

CustomEvent adds detail. Listeners read event.detail after you dispatch.

📈 Practical Patterns

Return values, synchronous order, and isTrusted.

Example 3 — Return Value & preventDefault()

Cancelable events return false when a listener calls preventDefault().

JavaScript
const target = new EventTarget();

target.addEventListener("save", (e) => {
  e.preventDefault();
});

const allowed = target.dispatchEvent(
  new Event("save", { cancelable: true })
);

console.log(allowed); // false
Try It Yourself

How It Works

Use this pattern for “may I proceed?” signals — listeners can veto by calling preventDefault() on a cancelable event.

Example 4 — Synchronous Delivery

Code after dispatchEvent runs only after listeners finish.

JavaScript
const target = new EventTarget();
const lines = [];

target.addEventListener("step", () => {
  lines.push("inside listener");
});

lines.push("before dispatch");
target.dispatchEvent(new Event("step"));
lines.push("after dispatch");

console.log(lines.join("\n"));
// before dispatch
// inside listener
// after dispatch
Try It Yourself

How It Works

That nested call stack is why MDN stresses synchrony — unlike many queued native events.

Example 5 — isTrusted Is False

Synthetic events are marked untrusted.

JavaScript
const target = new EventTarget();

target.addEventListener("probe", (e) => {
  console.log("type:", e.type);
  console.log("isTrusted:", e.isTrusted);
  console.log("target is EventTarget:", e.target instanceof EventTarget);
});

target.dispatchEvent(new Event("probe"));
// type: probe
// isTrusted: false
// target is EventTarget: true
Try It Yourself

How It Works

Security-sensitive code can ignore untrusted events. Your own app events are still perfectly valid for UI and state updates.

🚀 Common Use Cases

  • Custom app events — notify listeners when a model changes.
  • Component APIs — widgets that fire change / submit-style events.
  • Veto flows — cancelable events + check the boolean return.
  • Testing — simulate delivery without real user input (remember isTrusted).
  • Workers — same listen/dispatch pattern off the main thread.
  • Not a fake user click for security APIs — browsers treat synthetic events differently.

🧠 How dispatchEvent() Works

1

Create the event

new Event(type) or new CustomEvent(type, { detail }).

Create
2

Call dispatchEvent

Sets event.target to this EventTarget.

Dispatch
3

Run listeners

Matching handlers run in order (capture/bubble on DOM).

Listen
4

Return boolean

false if cancelable and prevented; otherwise true.

📝 Notes

  • Create the event before calling dispatchEvent.
  • Type strings are case-sensitive and must match listeners.
  • Delivery is synchronous — plan for nested call stacks.
  • Handler exceptions do not reject or throw to your dispatch call site.
  • Available in Web Workers (per MDN).
  • Related: addEventListener(), removeEventListener(), EventTarget().

Universal Browser Support

EventTarget.dispatchEvent() is Baseline Widely available across modern browsers and has been part of the DOM event model for a long time. It is also available in Web Workers.

Baseline · Widely available

EventTarget.dispatchEvent()

Safe for production. Create Event/CustomEvent first, then dispatch synchronously to your listeners.

Universal Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
dispatchEvent() Excellent

Bottom line: Use dispatchEvent to fire app events. Check the boolean return for cancelable flows, and remember isTrusted is false for synthetic events.

Conclusion

dispatchEvent() is the firing step of the EventTarget API: build an Event or CustomEvent, dispatch it, and listeners run immediately. Use the boolean return with cancelable events when listeners may call preventDefault().

Continue with removeEventListener(), addEventListener(), or Window methods.

💡 Best Practices

✅ Do

  • Create events with Event / CustomEvent constructors
  • Match type strings carefully with listeners
  • Use detail for structured payloads
  • Check the return value for cancelable workflows
  • Remember delivery is synchronous

❌ Don’t

  • Dispatch before the event is constructed
  • Expect isTrusted to be true for synthetic events
  • Assume handler errors bubble to your call site
  • Use dispatchEvent to fake trusted user gestures for restricted APIs
  • Forget cancelable: true when you need preventDefault

Key Takeaways

Knowledge Unlocked

Five things to remember about dispatchEvent()

Fire events synchronously on any EventTarget.

5
Core concepts
02

Sync

listeners first

Timing
📦 03

detail

CustomEvent

Data
04

Return

true / false

Cancel
🔒 05

isTrusted

false

Synthetic

❓ Frequently Asked Questions

EventTarget.dispatchEvent(event) sends an Event to that target and runs matching listeners immediately (synchronously). Create the event first with new Event() or new CustomEvent().
Yes. Unlike most native browser events that are queued, dispatchEvent runs applicable listeners before it returns. Code after dispatchEvent runs only after those listeners finish.
It returns false if the event is cancelable and a listener called preventDefault(). Otherwise it returns true.
Native browser events have isTrusted true. Events you fire with dispatchEvent have isTrusted false — they are synthetic.
Yes. When you call dispatchEvent, Event.target is initialized to the EventTarget you called it on.
A TypeError is thrown if the event type was not specified during initialization.
Did you know?

Even on a DOM element, an event you fire with dispatchEvent still gets isTrusted: false. Real user clicks stay true — that is how browsers tell “synthetic” from “native.”

Clean Up Listeners Next

Learn removeEventListener to detach handlers safely.

removeEventListener() →

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.

8 people found this page helpful