Skip to content

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

ParameterTypeDescription
eleTThe element to which the event listener should be added.
typekeyof HTMLElementEventMapThe type of event to listen for.
eventHandle(ev) => voidThe 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?booleanA 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?booleanA boolean indicating whether the event handler should be debounced.
options.useThrottle?booleanA 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

Released under the MIT License.