DOM: .insertAdjacentElement

By Xah Lee. Date: . Last updated: .

node.insertAdjacentElement(position, newEle)

Move node newEle to a position around node node.

It can insert {before, after} node as sibling, or as {first, last} child of node.

position is a string, and must be one of:

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

// a existing node
const x = document.getElementById("xyz");

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

back to DOM: Basic DOM Methods

Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Patreon me $5. Ask me question on patreon