Skip to content

Function: insertBefore()

ts
function insertBefore<T>(newNode, referenceNode): T;

Inserts a node before a reference node.

Type Parameters

Type Parameter
T extends Node

Parameters

ParameterTypeDescription
newNodeTThe node to insert.
referenceNodeNodeThe reference node.

Returns

T

The inserted node.

Example

ts
const parent = document.createElement('div')
const a = document.createElement('span')
const b = document.createElement('span')
parent.appendChild(a)
insertBefore(b, a)
parent.children.length // => 2

Since

1.0.0

Released under the MIT License.