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:

Return the element inserted, or null if failed.

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

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

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

back to DOM: Basic DOM Methods

Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc
  9. Web Dev Blog