Skip to content

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

ParameterTypeDescription
containerElementThe container element.
eventKThe event name.
selectorstringThe selector to match.
handler(event, target) => voidThe 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

Released under the MIT License.