Warning: 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
.
const event = document.createEvent("Event");
event.initEvent("build", true, true);
elem.addEventListener(
"build",
(e) => {
},
false,
);
elem.dispatchEvent(event);
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.