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 EventTarget.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

Specifications

Specification Status Comment
DOM
The definition of 'document.createEvent' in that specification.
Living Standard

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
createEventChrome Full support 1Edge Full support 12Firefox Full support 1
Notes
Full support 1
Notes
Notes From version 67, creating touch events using this method is no longer supported.
IE Full support 9Opera Full support 7Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

Full support
Full support
See implementation notes.
See implementation notes.

See also