DOM - Sibling Manipulation

About

This page is about Sibling manipulation in the DOM

Method

insertAdjacent

Example:

  • Insert an element
let bodySibling = document.createElement('p');
bodySibling.innerText = 'Body Sibling Element';
document.body.insertAdjacentElement('afterend', bodySibling);
  • or Insert HTML directly
document.body.insertAdjacentHTML('afterend', '<p>Body Sibling HTML</p>');
  • Result:

Syntax:

element.insertAdjacentElement(position, siblingElement);
element.insertAdjacentHTML(position, text);

where position is:

  • beforebegin
  • afterbegin
  • beforeend
  • afterend

ie

<!-- beforebegin -->
<element>
  <!-- afterbegin -->
  text
  <!-- beforeend -->
</element>
<!-- afterend -->

Selector

Documentation / Reference


Powered by ComboStrap