DOM: insertAdjacentElement

By Xah Lee. Date: . Last updated: .

node.insertAdjacentElement(position, new_node)

Move node new_node to a position around node node.

position is a string, and must be one of:

Before node.
After node.
As first child of node.
As last child of node.

Return the element inserted, or null if failed.

// create new node <p>hi</p>
const y = document.createElement("p");
y.textContent = "hi";

// a existing node
const x = document.getElementsByTagName ('p')[0] ;

// insert after
x. insertAdjacentElement("afterend", y);

back to DOM: Basic DOM Methods

JS in Depth