trigger a specific event on a given element, optionally passing custom data

PHOTO EMBED

Sat Feb 26 2022 02:21:05 GMT+0000 (UTC)

Saved by @mboljar

const triggerEvent = (el, eventType, detail) =>
  el.dispatchEvent(new CustomEvent(eventType, { detail }));

// Examples
triggerEvent(document.getElementById('myId'), 'click');
triggerEvent(document.getElementById('myId'), 'click', { username: 'bob' });
content_copyCOPY

https://madza.hashnode.dev/24-modern-es6-code-snippets-to-solve-practical-js-problems?guid