Skip to content

HTMLElement.onNodeInserted method

HTMLElement › onNodeInserted

Official

Adds an event listener to the element when it is inserted into the DOM.

Signature:

onNodeInserted(this: HTMLElement, listener: () => unknown, once?: boolean | undefined): () => void

Parameters:

Parameter Type Description
this HTMLElement
listener () => unknown the callback to call when this node is inserted into the DOM.
once? boolean | undefined if true, this will only fire once and then unhook itself.

Returns: () => voiddestroy - a function to remove the event handler to avoid memory leaks.

Example:

document.body.onNodeInserted(() => {
console.log('node inserted');
});