Kebab file names

This commit is contained in:
Angelos Chalaris
2023-04-27 21:58:35 +03:00
parent 1d189c709a
commit 61200d90c4
440 changed files with 0 additions and 0 deletions

23
snippets/trigger-event.md Normal file
View File

@ -0,0 +1,23 @@
---
title: Trigger event on HTML element
tags: browser,event
cover: cloudy-mountaintop-2
firstSeen: 2018-06-19T20:57:58+03:00
lastUpdated: 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.
```js
const triggerEvent = (el, eventType, detail) =>
el.dispatchEvent(new CustomEvent(eventType, { detail }));
```
```js
triggerEvent(document.getElementById('myId'), 'click');
triggerEvent(document.getElementById('myId'), 'click', { username: 'bob' });
```