JavaScript Document createEvent() Method

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

What You’ll Learn

document.createEvent() is a deprecated instance method that creates an Event object of a named interface type (see MDN Document: createEvent()). Learn the old create → init → dispatch flow, why MDN warns against it, and how modern constructors replace it — with five examples and try-it labs.

01

Kind

Instance method

02

Arg

type string

03

Returns

Event

04

Next step

init + dispatch

05

Prefer

new Event()

06

Status

Deprecated

Introduction

Before event constructors were widely available, scripts built synthetic events like this: create an empty event with document.createEvent(type), fill it with an init* method, then fire it with dispatchEvent.

That pattern still works in many browsers for compatibility, but MDN labels createEvent deprecated and warns that related init* APIs are deprecated too. Today you write new Event("name", { bubbles: true }) in one step.

💡
Learn it, don’t ship it

Study createEvent so you can read legacy code and interview questions. For new features, use constructors and dispatchEvent.

Related tutorials: createElement(), createElementNS().

Understanding document.createEvent()

An instance method on the page’s document object (MDN Document interface).

  • Parametertype: string naming the event interface family (MDN).
  • Return value — an Event object (MDN).
  • Not finished yet — you must initialize (for example initEvent) before dispatch.
  • Dispatch — pass the event to EventTarget.dispatchEvent (MDN).
  • Modern path — constructors create and configure events in one expression.
  • Status — deprecated; prefer constructors (MDN).

📝 Syntax

General form of Document.createEvent (MDN):

JavaScript
createEvent(type)

Parameters

  • type — string for the event interface to create. MDN examples include "UIEvents", "MouseEvents", "MutationEvents", and "HTMLEvents". The plain MDN sample uses "Event".

Return value

An Event object (MDN). Initialize it, then dispatch it.

MDN legacy pattern

JavaScript
// Create the event.
const event = document.createEvent("Event");

// Define that the event name is 'build'.
event.initEvent("build", true, true);

// Listen for the event.
elem.addEventListener("build", (e) => {
  // e.target matches elem
});

// Target can be any Element or other EventTarget.
elem.dispatchEvent(event);

Modern replacement

JavaScript
const event = new Event("build", { bubbles: true, cancelable: true });
elem.addEventListener("build", (e) => {
  // e.target matches elem
});
elem.dispatchEvent(event);

📋 Common type strings (legacy)

MDN notes that suitable type strings are listed in the DOM standard, and that most event objects now have constructors instead.

Legacy typeTypical useModern constructor
"Event"Generic custom eventsnew Event(name)
"HTMLEvents"Older HTML event objectsnew Event(name)
"MouseEvents"Synthetic mouse eventsnew MouseEvent(name, opts)
"UIEvents"UI-related eventsnew UIEvent(name, opts)
"MutationEvents"Legacy mutation eventsAvoid — use MutationObserver

⚡ Quick Reference

GoalCode
Legacy createdocument.createEvent("Event")
Legacy initevent.initEvent("build", true, true)
Dispatchelem.dispatchEvent(event)
Modern Eventnew Event("build", { bubbles: true })
Modern CustomEventnew CustomEvent("build", { detail: 42 })
MDN statusDeprecated

🔍 At a Glance

Four facts about document.createEvent().

Returns
Event

must init

Status
deprecated

MDN

Replace
new Event()

constructors

Fire with
dispatchEvent

same as modern

📋 Legacy flow vs constructor flow

StepLegacy createEventModern constructor
1. CreatecreateEvent("Event")new Event("build", opts)
2. ConfigureinitEvent(...)Done in constructor options
3. ListenaddEventListenerSame
4. FiredispatchEventSame

Examples Gallery

Examples follow MDN Document: createEvent() and show the modern replacement you should prefer.

📚 Getting Started

The classic MDN sample and its one-line modern twin.

Example 1 — MDN: createEvent + initEvent + dispatch

Legacy three-step pattern from MDN (recognize it; do not use in new apps).

JavaScript
const elem = document.getElementById("box");

const event = document.createEvent("Event");
event.initEvent("build", true, true);

elem.addEventListener("build", () => {
  console.log("build fired on", elem.id);
});

elem.dispatchEvent(event);
// "build fired on box"
Try It Yourself

How It Works

createEvent allocates an event shell; initEvent(name, bubbles, cancelable) configures it; dispatchEvent delivers it to listeners.

Example 2 — Modern replacement with new Event()

Same behavior without the deprecated factory.

JavaScript
const elem = document.getElementById("box");

const event = new Event("build", { bubbles: true, cancelable: true });

elem.addEventListener("build", () => {
  console.log("modern build fired");
});

elem.dispatchEvent(event);
// "modern build fired"
Try It Yourself

How It Works

The constructor sets the event type and options in one call. dispatchEvent stays the same API for both old and new styles.

📈 Practical Patterns

Custom payloads, mouse events, and bubbling flags.

Example 3 — Prefer CustomEvent for data

Pass a detail payload without deprecated initCustomEvent.

JavaScript
const elem = document.getElementById("box");

elem.addEventListener("order", (e) => {
  console.log("qty:", e.detail.qty);
});

const event = new CustomEvent("order", {
  detail: { qty: 3 },
  bubbles: true
});

elem.dispatchEvent(event);
// "qty: 3"
Try It Yourself

How It Works

MDN warns that many init* methods used with createEvent are deprecated. CustomEvent is the clean modern way to attach data.

Example 4 — Legacy MouseEvents vs MouseEvent

Side-by-side: old factory type string versus the modern mouse constructor.

JavaScript
// Legacy (deprecated path — for reading old code only):
const legacy = document.createEvent("MouseEvents");
legacy.initMouseEvent(
  "click", true, true, window,
  0, 0, 0, 0, 0,
  false, false, false, false,
  0, null
);

// Modern:
const modern = new MouseEvent("click", {
  bubbles: true,
  cancelable: true,
  view: window
});

console.log(legacy.type, modern.type); // "click" "click"
console.log(modern instanceof MouseEvent); // true
Try It Yourself

How It Works

initMouseEvent needs a long argument list. new MouseEvent uses a readable options object instead.

Example 5 — Bubbling flags with initEvent

Legacy initEvent(type, bubbles, cancelable) matches constructor options.

JavaScript
const parent = document.getElementById("parent");
const child = document.getElementById("child");

parent.addEventListener("ping", () => console.log("parent heard ping"));

const event = document.createEvent("Event");
event.initEvent("ping", true, false); // bubbles: true, cancelable: false

child.dispatchEvent(event);
// "parent heard ping"  (because bubbles is true)

console.log(event.bubbles, event.cancelable); // true false
Try It Yourself

How It Works

When bubbles is true, the event climbs ancestors after the target phase. Modern code sets the same flag in constructor options.

🚀 Common Use Cases

  • Reading legacy tutorials — recognize create → init → dispatch samples.
  • Migrating old apps — replace createEvent with constructors.
  • Teaching event objects — show that synthetic events still use dispatchEvent.
  • Not for new features — do not introduce createEvent in fresh code (MDN).
  • Custom component messaging — use CustomEvent + detail.
  • Automated UI tests — prefer MouseEvent / KeyboardEvent constructors.

🧠 How createEvent() Works (legacy)

1

Pass an interface type

MDN: a string such as "Event" or "MouseEvents".

Create
2

Initialize the event

Call initEvent (or another init* method) with name and flags.

Init
3

Attach listeners

Use addEventListener on the target (or an ancestor if bubbling).

Listen
4

Prefer constructors

Ship new Event / new CustomEvent instead of this path.

📝 Notes

  • MDN: Deprecated — avoid in new code.
  • MDN warning: many methods used with createEvent (such as initCustomEvent) are also deprecated.
  • Returned objects must be initialized before dispatchEvent (MDN).
  • Most event objects now have constructors — the recommended approach (MDN Notes).
  • dispatchEvent itself is still the modern way to fire synthetic events.
  • Related: createElement(), createElementNS().

Browser Support

Document.createEvent() is Deprecated on MDN. Logos use the shared browser-image-sprite.png sprite from this project. Engines may still expose it for compatibility — do not rely on it for new features.

Deprecated · Prefer constructors

Document.createEvent()

Legacy event factory — still present in many browsers, but MDN recommends Event constructors instead.

Legacy Compatibility only
Google Chrome Supported (deprecated)
Legacy
Mozilla Firefox Supported (deprecated)
Legacy
Apple Safari Supported (deprecated)
Legacy
Microsoft Edge Supported (deprecated)
Legacy
Opera Supported (deprecated)
Legacy
Internet Explorer Legacy support
Legacy
createEvent() Avoid in new code

Bottom line: Recognize createEvent in old samples. For new UI, use new Event / CustomEvent / MouseEvent and dispatchEvent.

Conclusion

document.createEvent(type) is the old factory for synthetic events. MDN marks it deprecated; initialize-and-dispatch helpers around it are deprecated too. Keep the pattern in mind for legacy code, and write new events with constructors.

Continue with createElementNS(), createExpression(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use new Event() / new CustomEvent() in new code
  • Pass options objects for bubbles and cancelable
  • Fire events with dispatchEvent
  • Replace createEvent when you touch legacy files
  • Learn the old API only to recognize it

❌ Don’t

  • Use createEvent in new production features
  • Rely on deprecated initCustomEvent / long initMouseEvent lists
  • Forget to initialize a createEvent result before dispatch
  • Confuse creating an event with listening for one
  • Assume every browser will keep the factory forever

Key Takeaways

Knowledge Unlocked

Five things to remember about createEvent()

Deprecated factory — prefer Event constructors.

5
Core concepts
⚠️02

Status

deprecated

legacy
🔧03

Needs

init then dispatch

3 steps
04

Prefer

new Event()

modern
🚀05

Fire

dispatchEvent

same API

❓ Frequently Asked Questions

MDN: Document.createEvent() creates an Event object of the type you name (for example "Event" or "MouseEvents"). You then initialize it (for example with initEvent) and pass it to EventTarget.dispatchEvent.
Yes. MDN marks Document.createEvent() as Deprecated. Many related init* methods (such as initCustomEvent) are also deprecated. Prefer event constructors like new Event() or new CustomEvent().
Use modern constructors: new Event("name"), new CustomEvent("name", { detail }), new MouseEvent("click", options), then call target.dispatchEvent(event).
An Event object (MDN). It is not ready to dispatch until you initialize it with the matching init method (for example initEvent).
MDN mentions types such as "UIEvents", "MouseEvents", "MutationEvents", and "HTMLEvents". The DOM standard lists createEvent type strings; most event objects now have constructors instead.
Only enough to recognize legacy code. New projects should create events with constructors and dispatch them with dispatchEvent.
Did you know?

dispatchEvent is not deprecated — only the old factory that builds events is. Modern apps still create synthetic events every day; they just start with new Event(...) instead of document.createEvent(...).

Next: createExpression()

Learn how to compile XPath strings into reusable XPathExpression objects.

createExpression() →

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.

6 people found this page helpful