new CustomEvent()
to create an event from the specified eventType
and details.EventTarget.dispatchEvent()
to trigger the newly created event on the given element.detail
, if you do not want to pass custom data to the triggered event.const triggerEvent = (el, eventType, detail) =>
el.dispatchEvent(new CustomEvent(eventType, { detail }));
triggerEvent(document.getElementById('myId'), 'click'); triggerEvent(document.getElementById('myId'), 'click', { username: 'bob' });
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️