Function: delegate()
ts
function delegate<K>(
container,
event,
selector,
handler): () => void;Adds a delegated event listener to a container.
Type Parameters
| Type Parameter |
|---|
K extends keyof GlobalEventHandlersEventMap |
Parameters
| Parameter | Type | Description |
|---|---|---|
container | Element | The container element. |
event | K | The event name. |
selector | string | The selector to match. |
handler | (event, target) => void | The delegated handler. |
Returns
A cleanup function.
() => void
Example
ts
const ul = document.createElement('ul')
document.body.appendChild(ul)
const off = delegate(ul, 'click', 'li', (event, target) => console.log(target))
console.log(typeof off)Since
1.0.0