W3cubDocs

/DOM

document.createEvent

Many methods used with createEvent, such as initCustomEvent, are deprecated. Use event constructors instead.

Creates an event of the type specified. The returned object should be first initialized and can then be passed to element.dispatchEvent.

Syntax

var event = document.createEvent(type);
  • event is the created Event object.
  • type is a string that represents the type of event to be created. Possible event types include "UIEvents", "MouseEvents", "MutationEvents", and "HTMLEvents". See Notes section for details.

Example

// Create the event.
var event = document.createEvent('Event');

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

// Listen for the event.
elem.addEventListener('build', function (e) {
  // e.target matches elem
}, false);

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

Notes

Event type strings suitable for passing to createEvent() are listed in the DOM standard — see the table in step 2. Bear in mind that most event objects now have constructors, which are the modern recommended way to create event object instances.

Gecko supports some non-standard event object aliases, which are listed below.

Event Module Standard event object Gecko also supports
Text event module TextEvent TextEvents
Keyboard event module KeyboardEvent KeyEvents
Basic events module Event Events

Specification

Browser compatibilityUpdate compatibility data on GitHub

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support Yes ? Yes ? ? ?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes Yes ? Yes ? ? ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/document/createEvent