Skip to content

HTMLElement.on method

HTMLElement › on

Official

Adds an event listener to the element.

Signature:

on(this: HTMLElement, type: K, selector: string, listener: (this: HTMLElement, ev: HTMLElementEventMap[K], delegateTarget: HTMLElement) => unknown, options?: boolean | AddEventListenerOptions | undefined): void

Parameters:

Parameter Type Description
this HTMLElement The element to add the event listener to.
type K The type of event to listen for.
selector string The selector of the event target.
listener (this: HTMLElement, ev: HTMLElementEventMap[K], delegateTarget: HTMLElement) => unknown The listener to call when the event is triggered.
options? boolean | AddEventListenerOptions | undefined The options of the event listener.

Returns: void

Example:

document.body.on('click', 'div', (ev) => {
console.log(ev);
});