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 thecompatibility 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 inWeb Workers.
TheEvent.initEvent() method is used to initialize thevalue of anevent created usingDocument.createEvent().
Events initialized in this way must have been created with theDocument.createEvent() method.This method must be called to set the eventbefore it is dispatched, usingEventTarget.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, likeEvent().The section onCreating and dispatching events gives more information about the way to use these.
In this article
Syntax
initEvent(type, bubbles, cancelable)Parameters
typeA string defining the type of event.
bubblesA boolean value deciding whether the event should bubble up through theevent chain or not. Once set, the read-only property
Event.bubbleswill give its value.cancelableA boolean value defining whether the event can be canceled. Once set, theread-only property
Event.cancelablewill give its value.
Return value
None.
Example
// Create the event.const event = document.createEvent("Event");// Create a click event that bubbles up and// cannot be canceledevent.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
See also
- The constructor to use instead of this deprecated method:
Event(). To create more specific event interfaces thanEvent, use the constructor defined for the desired event interface.