Files
30-seconds-of-code/snippets/triggerEvent.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

804 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Trigger event on HTML element browser,event cloudy-mountaintop-2 2018-06-19T20:57:58+03:00 2020-10-22T20:24:44+03:00

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

  • Use the CustomEvent constructor to create an event from the specified eventType and details.
  • Use EventTarget.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) =>
  el.dispatchEvent(new CustomEvent(eventType, { detail }));
triggerEvent(document.getElementById('myId'), 'click');
triggerEvent(document.getElementById('myId'), 'click', { username: 'bob' });