UseOn
katanakit-js / useOn
Function: useOn()
Section titled “Function: useOn()”function useOn<K>( target, event, callback, options?): (() => void) | null;Defined in: infrastructure/dom/dom.service.ts:362
Attaches an event listener to the target element and returns a cleanup function.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
K extends keyof HTMLElementEventMap |
The event type key from HTMLElementEventMap. |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
target |
string | EventTarget |
An EventTarget instance or a CSS selector string. |
event |
K |
The event name to listen for. |
callback |
(event) => void |
The event handler. |
options? |
boolean | AddEventListenerOptions |
Optional addEventListener options. |
Returns
Section titled “Returns”(() => void) | null
A function that removes the listener, or null if not in a browser.
Example
Section titled “Example”const off = useOn("#btn", "click", (e) => console.log("clicked", e));// later:off?.();