What is InsertAdjacent and InsertAdjacentHTML and how to use them ?

About

InsertAdjacent is a function that permits:

  • to insert:
    • a node (insertAdjacent)
    • or html string (insertAdjacentHTML)
  • into the DOM tree
  • relatively to a node
  • at this positions (as a child or sibling)

Insert Adajcent Illustration

Example

Insert an element (insertAdjacentElement)

Demo:

let pSibling = document.createElement('p');
pSibling.innerText = 'A paragraph';
document.body.insertAdjacentElement('afterbegin', pSibling);
  • insertAdjacentElement is a advanced version of appendChild and therefore execute script node. Adding an hello world script.
let bodySibling = document.createElement('script');
bodySibling.text = 'console.log("Hello World !");';
document.body.insertAdjacentElement('beforeend', bodySibling);
  • Result:

Insert HTML directly (insertAdjacentHTML)

document.body.insertAdjacentHTML('afterend', '<p>Body Sibling HTML</p>');
  • Result:



insertAdjacentHTML is an advanced version of InnerHtml and therefore does not execute any script.

Syntax

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

where position is:

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

Security

insertAdjacentHTML is an advanced version of InnerHtml and therefore does not execute any script while insertAdjacentElement will execute them.





Discover More
DOM - Child Nodes of a Node

Child Nodes of an element in the dom tree Via child selector, you can select element that are child of another element. The childNodes property of an element returns a collection of child Node....
DOM - Sibling Manipulation

This page is Sibling manipulation in the DOM can insert at this positions: See Sibling selector insertAdjacentElement...
Color Autocompletion List
How to create an Autocompletion functionality with CSS , Javascript and HTML ?

This article shows you how to create a search box with an autocompletion feature
How to load a script dynamically with Javascript?

This page shows you how you can load a script dynamically with Javascript to load and use it when needed. In short, if you add: a script via an DOM element, it will be executed (via append or insertAdjacentElement)...
What is an HTML Fragment ?

A fragment in HTML is a piece of HTML that is intended to be added dynamically into an web page. Via insertAdjacentHTML) The fragment (You could have get it from an AJAX call) Adding it in the...
What is the innerHTML property and how does it work ?

innerHTML is a property of a node element that permits to set the whole descendants of this node with an HTML fragment outerHTML HTML fragmentInsertAdjacentHTML script elements inserted using...
What is the outerHTML property and how does it work ?

outerHTML is a property of a node element that permits to replace the target element with an HTML fragment innerHTML HTML fragmentInsertAdjacentHTML script elements inserted using outerHTML...



Share this page:
Follow us:
Task Runner