Saltar al contenido principal

Function: useOn()

useOn<K>(target, event, callback, options?): (() => void) | null

Defined in: src/infrastructure/dom/dom.service.ts:340

Attaches an event listener to the target element and returns a cleanup function.

Type Parameters

K

K extends keyof HTMLElementEventMap

The event type key from HTMLElementEventMap.

Parameters

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

(() => void) | null

A function that removes the listener, or null if not in a browser.

Example

const off = useOn("#btn", "click", (e) => console.log("clicked", e));
// later:
off?.();