Skip to content

Files

Latest commit

2429509 · Jun 19, 2018

History

History
17 lines (13 loc) · 647 Bytes

triggerEvent.md

File metadata and controls

17 lines (13 loc) · 647 Bytes

triggerEvent

Triggers a specific event on a given element, optionally passing custom data.

Use new CustomEvent() to create an event from the specified eventType and details. Use el.dispatchEvent() to trigger the newly created event on the given element. Omit the third argument, detail, if you do not want to pass custom data to the triggered event.

const triggerEvent = (el, eventType, detail = undefined) =>
  el.dispatchEvent(new CustomEvent(eventType, { detail: detail }));
triggerEvent(document.getElementById('myId'), 'click');
triggerEvent(document.getElementById('myId'), 'click', { username: 'bob' });