MDN self-hosted library · e01.org

Event: initEvent() method

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

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);