This page is Almost Ready

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

createEvent

Summary

Creates a DOM event of the specified type. This method is deprecated; use event constructors (CustomEvent) instead.

Method of dom/Documentdom/Document

Syntax

var event = document.createEvent(eventType);

Parameters

eventType

Data-type
String

One of the following values. Case is not important.

Return Value

Returns an object of type DOM NodeDOM Node

The created event.

Examples

The following example demonstrates how to create and dispatch a custom event that bubbles and cannot be canceled.

var evt = document.createEvent("Event");
evt.initEvent("custom", true, false);
document.getElementById('target').dispatchEvent(evt);

To respond to the custom event created previously, the following example adds an event handler that interacts with the event by setting a expando property named detail.

function reportEvent(evt) {
    evt.detail = "Success.";
}
var p = document.getElementById('target');
p.addEventListener("custom", reportEvent, false);

Notes

If the event object is to be dispatched with dispatchEvent, the appropriate event initialization method must be called. For example, after creating an event of type UIEvent, call initUIEvent to initialize the event object’s values. **Security Warning: **For security reasons, events generated with createEvent are untrusted and have a isTrusted value of false.

Related specifications

DOM Level 3 Events
Working Draft

Attributions