Event: initEvent() method

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Note: This feature is available in Web Workers.

The Event.initEvent() method is used to initialize the value of an event created using Document.createEvent().

Events initialized in this way must have been created with the Document.createEvent() method. This method must be called to set the event before it is dispatched, using EventTarget.dispatchEvent(). Once dispatched, it doesn't do anything anymore.

Note: Do not use this method anymore as it is deprecated. Instead use specific event constructors, like Event(). The section on Creating and dispatching events gives more information about the way to use these.

Syntax

js
initEvent(type, bubbles, cancelable)

Parameters

type

A string defining the type of event.

bubbles

A boolean value deciding whether the event should bubble up through the event chain or not. Once set, the read-only property Event.bubbles will give its value.

cancelable

A boolean value defining whether the event can be canceled. Once set, the read-only property Event.cancelable will give its value.

Return value

None.

Example

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

// Create a click event that bubbles up and
// cannot be canceled
event.initEvent("click", true, false);

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

elem.dispatchEvent(event);

Specifications

Specification
DOM
# dom-event-initevent

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
initEvent
Deprecated
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Partial support
Firefox 1 – 16 (Release date: 2004-11-09)
footnote Partial support
footnote Before Firefox 17, a call to this method after the dispatching of the event raised an exception instead of doing nothing.
Firefox – Full support
Firefox 17 (Release date: 2012-11-20)
footnote Full support
Opera – Full support
Opera 12.1 (Release date: 2012-11-20)
footnote Full support
Safari – Full support
Safari 1 (Release date: 2003-06-23)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Partial support
Firefox for Android 4 – 16 (Release date: 2011-03-29)
footnote Partial support
footnote Before Firefox for Android 17, a call to this method after the dispatching of the event raised an exception instead of doing nothing.
Firefox for Android – Full support
Firefox for Android 17 (Release date: 2012-11-20)
footnote Full support
Opera Android – Full support
Opera Android 12.1 (Release date: 2012-10-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – No support
Deno
footnote No support
Node.js – Full support
Node.js 19.5 (Release date: 2023-01-24)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
Deprecated. Not for use in new websites.
Has more compatibility info.

See also

  • The constructor to use instead of this deprecated method: Event(). To create more specific event interfaces than Event, use the constructor defined for the desired event interface.