Categories
JavaScript

Move element

  • Add as the last child node of the parent element
element.appendChild(childNode)

https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild

  • Add as the first child node of the parent element
element.prepend(childNode)

https://developer.mozilla.org/en-US/docs/Web/API/Element/prepend

  • Insert at the specified position
element.insertAdjacentHTML(position, text);
  1. Position:
    • 'beforebegin': Before the element itself.
    • 'afterbegin': Just inside the element, before its first child.
    • 'beforeend': Just inside the element, after its last child.
    • 'afterend': After the element itself.
<!-- beforebegin -->
<p>
  <!-- afterbegin -->
  foo
  <!-- beforeend -->
</p>
<!-- afterend -->

https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML

Leave a comment