JavaScript Event initEvent() Method

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

What You’ll Learn

The initEvent() method initializes an event created with document.createEvent() by setting type, bubbles, and cancelable before dispatchEvent(). Learn the old pattern, why it is deprecated, and how to migrate to new Event()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

undefined

03

Args

type, bubbles, cancelable

04

Pairs with

createEvent

05

Modern

new Event()

06

Status

Deprecated

Introduction

Years ago, creating a synthetic event often meant two steps: document.createEvent("Event"), then event.initEvent(type, bubbles, cancelable), then dispatchEvent.

Today you write one expression: new Event("click", { bubbles: true, cancelable: false }). Same idea—name, bubble flag, cancel flag—cleaner API. This tutorial shows the old method so you can recognize it in legacy codebases and replace it confidently.

💡
Beginner tip

Call initEvent() before dispatchEvent(). After dispatch, MDN notes that calling it again does not re-initialize the dispatched event in a useful way.

Understanding Event.initEvent()

An instance method that sets the core flags on an event created with Document.createEvent().

  • type — string event name (for example "click").
  • bubbles — whether the event bubbles; sets event.bubbles.
  • cancelable — whether preventDefault() can cancel it; sets event.cancelable.
  • Return value — none (undefined).
  • Status — Deprecated on MDN; available in Web Workers, but avoid for new code.

📝 Syntax

JavaScript
initEvent(type, bubbles, cancelable)

Parameters

ParameterMeaning
typeString defining the type of event
bubblesBoolean—should the event bubble?
cancelableBoolean—can the event be canceled?

Return value

None (undefined).

Legacy pattern (avoid in new code)

JavaScript
const event = document.createEvent("Event");
event.initEvent("click", true, false);
elem.dispatchEvent(event);

Modern replacement

JavaScript
const event = new Event("click", {
  bubbles: true,
  cancelable: false
});
elem.dispatchEvent(event);

⚡ Quick Reference

GoalCode / note
Legacy initevent.initEvent("click", true, false)
Modern createnew Event("click", { bubbles: true, cancelable: false })
Dispatchelem.dispatchEvent(event)
Read flagsevent.type, event.bubbles, event.cancelable
MDN statusDeprecated — do not use in new code

🔍 At a Glance

Four facts to remember about initEvent().

Status
deprecated

Avoid new use

Args
3

type, bubbles, cancelable

Returns
void

No return value

Prefer
Event()

Constructor API

Examples Gallery

Examples follow MDN Event: initEvent(). Legacy demos are for learning and migration—prefer constructors in real apps.

📚 Getting Started

Recognize the createEvent + initEvent + dispatch flow.

Example 1 — Legacy createEvent + initEvent

MDN-style pattern: create, initialize, listen, dispatch.

JavaScript
const event = document.createEvent("Event");
event.initEvent("click", true, false);

elem.addEventListener("click", (e) => {
  console.log("heard", e.type);
});

elem.dispatchEvent(event);
Try It Yourself

How It Works

This is the old factory style. New code should skip straight to new Event().

Example 2 — Read type, bubbles, cancelable

After initEvent, the matching properties reflect your arguments.

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

console.log(event.type);       // "ping"
console.log(event.bubbles);    // true
console.log(event.cancelable); // true
Try It Yourself

How It Works

Those three arguments map directly onto the read-only-style event flags you use later.

📈 Modern Migration

Prefer constructors; compare bubbling and side-by-side rewrites.

Example 3 — Prefer new Event()

Same outcome without createEvent or initEvent.

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

elem.addEventListener("click", () => console.log("modern click"));
elem.dispatchEvent(event);
Try It Yourself

How It Works

Constructor options replace the three initEvent arguments in one step.

Example 4 — Bubbling with bubbles: true

A parent listener hears a dispatched event when bubbles is true.

JavaScript
// Legacy:
const legacy = document.createEvent("Event");
legacy.initEvent("notify", true, false);
child.dispatchEvent(legacy);

// Modern equivalent:
child.dispatchEvent(new Event("notify", { bubbles: true }));
Try It Yourself

How It Works

Both paths set the same bubbling behavior; only the creation API differs.

Example 5 — Side-by-Side Migration Checklist

Map old calls to constructor options one-to-one.

JavaScript
// Before (deprecated):
// event.initEvent("save", true, true);

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

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

How It Works

Argument order (type, bubbles, cancelable) becomes named options on the constructor.

🚀 Common Use Cases

  • Reading or maintaining legacy code that still uses createEvent.
  • Migrating old synthetic-event helpers to Event() / CustomEvent().
  • Teaching how type, bubbles, and cancelable were set historically.
  • Comparing deprecated APIs with Baseline constructor APIs.
  • Auditing libraries for initEvent / createEvent usage.

🔧 How It Works

1

createEvent

Legacy factory builds an uninitialized Event object.

Legacy
2

initEvent

Sets type, bubbles, and cancelable on that object.

Init
3

dispatchEvent

Listeners run; re-init after dispatch is not useful.

Fire
4

Migrate

Replace both steps with new Event(type, options).

📝 Notes

  • MDN: Deprecated — Deprecated banner shown before “What You’ll Learn”.
  • Not marked Experimental or Non-standard on MDN (deprecated only).
  • Intended for events from document.createEvent().
  • Available in Web Workers, but still deprecated.
  • Related learning: Event(), bubbles, cancelable, type, dispatchEvent(), composedPath().

Browser Support (Legacy)

Event.initEvent() is marked Deprecated on MDN. Many browsers still implement it for compatibility with old pages, but you should not rely on it for new work. Logos use the shared browser-image-sprite.png sprite from this project. Prefer new Event().

Deprecated · Prefer Event()

Event.initEvent()

Legacy initializer for createEvent events. Use Event() (or a more specific constructor) instead.

Legacy Deprecated API
Google Chrome Still present for legacy pages; prefer constructors
Legacy
Mozilla Firefox Still present for legacy pages; prefer constructors
Legacy
Apple Safari Still present for legacy pages; prefer constructors
Legacy
Microsoft Edge Still present for legacy pages; prefer constructors
Legacy
Opera Still present for legacy pages; prefer constructors
Legacy
Internet Explorer Historically used createEvent / initEvent heavily
Legacy
Event.initEvent() Avoid in new code

Bottom line: Recognize initEvent in old code, then migrate to new Event(type, { bubbles, cancelable }).

Conclusion

Event.initEvent() is a deprecated way to set type, bubbles, and cancelable on events from createEvent. Learn it to read old code; ship new features with new Event() (or a more specific constructor).

Continue with preventDefault(), Event(), composedPath(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Prefer new Event() / CustomEvent()
  • Map (type, bubbles, cancelable) to constructor options
  • Feature-detect only if you must support ancient browsers
  • Document migrations when removing initEvent
  • Use this page to understand legacy snippets

❌ Don’t

  • Write new production code with initEvent
  • Rely on re-calling it after dispatchEvent
  • Mix createEvent and constructor styles in one helper
  • Forget that constructors can set more options (e.g. composed)
  • Teach beginners initEvent as the default pattern

Key Takeaways

Knowledge Unlocked

Five things to remember about initEvent()

Legacy initializer—migrate to constructors.

5
Core concepts
🔧02

3 args

type / bubbles / cancelable

API
📝03

createEvent

legacy partner

Pair
04

Event()

modern replace

Prefer
🚀05

dispatch

after init

Flow

❓ Frequently Asked Questions

It initializes an event created with document.createEvent() by setting type, bubbles, and cancelable before you call dispatchEvent(). Once the event has been dispatched, calling initEvent() again does nothing useful.
Yes. MDN marks Event.initEvent() as Deprecated. Prefer event constructors such as new Event("click", { bubbles: true, cancelable: false }).
Use new Event(type, options), or a more specific constructor (CustomEvent, MouseEvent, KeyboardEvent, and so on). Then dispatch with target.dispatchEvent(event).
Modern code should not need initEvent() at all. The constructor options already set type, bubbles, and cancelable. initEvent() was paired with the older document.createEvent() API.
type (string event name), bubbles (boolean), and cancelable (boolean). After init, you can read event.type, event.bubbles, and event.cancelable.
Yes. MDN notes the feature is available in Web Workers, but it remains deprecated—prefer constructors there too when available.
Did you know?

Modern constructors can set flags initEvent never covered, such as composed for Shadow DOM. That is another reason to prefer new Event("notify", { bubbles: true, composed: true }) over the old two-step API.

Next: preventDefault()

Cancel link, form, and checkbox defaults the modern way.

preventDefault() →

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.

5 people found this page helpful