Function: addEvent()
ts
function addEvent<T>(
ele,
type,
eventHandle,
options?): (() => void) | undefined;Adds an event listener to a given element.
Type Parameters
| Type Parameter |
|---|
T extends HTMLElement | SVGElement |
Parameters
| Parameter | Type | Description |
|---|---|---|
ele | T | The element to which the event listener should be added. |
type | keyof HTMLElementEventMap | The type of event to listen for. |
eventHandle | (ev) => void | The function to be called when the event occurs. |
options | { useCapture?: boolean; useDebounce?: boolean; useThrottle?: boolean; } | Additional options to modify the behavior of the event listener. |
options.useCapture? | boolean | A boolean indicating whether events of this type should be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree. |
options.useDebounce? | boolean | A boolean indicating whether the event handler should be debounced. |
options.useThrottle? | boolean | A boolean indicating whether the event handler should be throttled. |
Returns
(() => void) | undefined
A function to remove the event listener.
Example
ts
const button = document.createElement('button')
document.body.appendChild(button)
addEvent(button, 'click', () => console.log('Button clicked!'))
button.click()Since
1.0.0