// 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);Event: initEvent() method / notebook
Event: initEvent() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.