Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Document
  4. createEvent()

Document: createEvent() 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.

Warning:Many methods used withcreateEvent, such asinitCustomEvent, are deprecated.Useevent constructors instead.

Creates anevent of the type specified. Thereturned object should be first initialized and can then be passed toEventTarget.dispatchEvent.

Syntax

js
createEvent(type)

Parameters

type

A string that represents the type of event to be created. Possible event types include"UIEvents","MouseEvents","MutationEvents", and"HTMLEvents". SeeNotes section for details.

Return value

AnEvent object.

Examples

js
// Create the event.const event = document.createEvent("Event");// Define that the event name is 'build'.event.initEvent("build", true, true);// Listen for the event.elem.addEventListener("build", (e) => {  // e.target matches elem});// Target can be any Element or other EventTarget.elem.dispatchEvent(event);

Notes

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

Specifications

Specification
DOM
# dom-document-createevent

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp