JavaScript Document createEvent() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.createEvent(type) creates an Event object of a named interface family. You must initialize it (for example with initEvent()), then fire it with dispatchEvent().

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

Syntax

Call document.createEvent. Start with this line.

JavaScript
document.createEvent(type)
CallWhat it does
document.createEvent(type)Primary signature.
document.createEvent(...)Call with the arguments your case needs.

Built into the browser. Call createEvent(...) on document — no import required.

Library declarations
JavaScript
document.createEvent(type)

Parameters

ParameterDescription
typeRequired. Interface family string (e.g. "Event", "MouseEvents")
Init stepRequired. Sets event name, bubbles, cancelable (via init*)

Return Value

Return ValueDescription
*Event object (dispatch yourself)

JavaScript Examples

Example 1 — Legacy createEvent + initEvent

MDN-style create → init → dispatch for a custom "build" event.

JavaScript
const elem = document.createElement("div");

elem.addEventListener("build", function () {
  console.log("build fired");
});

const event = document.createEvent("Event");
event.initEvent("build", true, true);
elem.dispatchEvent(event);
// "build fired"
Try It Yourself

How It Works

  1. Listen first. Register a "build" listener on the target.
  2. Create. createEvent("Event") returns an empty Event shell.
  3. Init. initEvent("build", true, true) sets name, bubbles, cancelable.
  4. Dispatch. dispatchEvent runs the listener.

Example 2 — Prefer new Event()

Same result in one constructor call — the recommended path.

JavaScript
const elem = document.createElement("div");

elem.addEventListener("build", function (e) {
  console.log(e.type, e.bubbles, e.cancelable);
});

const event = new Event("build", { bubbles: true, cancelable: true });
elem.dispatchEvent(event);
// "build" true true
Try It Yourself

How It Works

  1. Skip createEvent. Constructors configure the event up front.
  2. Pass options. bubbles and cancelable replace initEvent args.
  3. Dispatch the same way. Listeners do not care how the event was built.
  4. Use this in apps. Keep createEvent for reading old code only.

Example 3 — MouseEvents vs MouseEvent

Legacy type string versus the modern mouse constructor.

JavaScript
const btn = document.createElement("button");
let legacyType = "";
let modernType = "";

btn.addEventListener("click", function (e) {
  if (!legacyType) legacyType = e.constructor.name;
  else modernType = e.constructor.name;
});

const legacy = document.createEvent("MouseEvents");
legacy.initEvent("click", true, true);
btn.dispatchEvent(legacy);

const modern = new MouseEvent("click", { bubbles: true, cancelable: true });
btn.dispatchEvent(modern);

console.log(legacyType);  // "MouseEvent"
console.log(modernType);  // "MouseEvent"
Try It Yourself

How It Works

  1. Legacy string. "MouseEvents" (plural) is the old createEvent type.
  2. Init still required. Without initEvent, the event is not ready.
  3. Modern constructor. new MouseEvent sets type and options together.
  4. Same interface. Both dispatch as MouseEvent instances to listeners.

Browser Support

Document.createEvent() remains widely implemented for compatibility, but MDN marks it Deprecated. Prefer event constructors in new code.

✓ Deprecated · Legacy DOM

Document.createEvent()

Still present in Chrome, Firefox, Safari, and Edge for legacy scripts. Do not rely on it for new features — use Event / CustomEvent / MouseEvent constructors.

Legacy Deprecated API
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
createEvent() Avoid in new code

Bottom line: Supported for compatibility, not recommended. Prefer new Event() and dispatchEvent(). See the MDN createEvent() reference.

Frequently Asked Questions

It 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. Related init* methods (such as initCustomEvent) are also deprecated. Prefer constructors like new Event() or new CustomEvent().
Use modern constructors: new Event("name"), new CustomEvent("name", { detail }), or new MouseEvent("click", options), then call target.dispatchEvent(event).
An Event object. 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.
No. Learn it only to read legacy code. New projects should create events with constructors and dispatch them with dispatchEvent.

Next: createExpression()

Learn document.createExpression() for XPath expressions (related Document method).

createExpression() tutorial →

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.

11 people found this page helpful