English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM insertAdjacentHTML() method

HTML DOM Element Object

insertAdjacentHTML()This method parses the specified text as HTML and inserts the resulting node at the specified position.

This method does not reparse the element being used, so it does not destroy the existing elements within the element.

This avoids additional serialization steps, making it faster than directinnerHTMLFaster operation.

Syntax:

element.insertAdjacentHTML(position, text)
var head = document.getElementsByTagName("h2")[0];
head.insertAdjacentHTML("afterend", "<span>Hello world<"/span>
Test See‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the insertAdjacentHTML() method:

Method
insertAdjacentHTML()is48isis8

Parameter value

ParameterDescription
positionRelative position to the element.
Legal value:
  • "afterbegin"-after the start of the element (as the first child)

  • "afterend"-after the element

  • "beforebegin"-before the element

  • "beforeend"-before the end of the element (as the last child)

textString to be parsed as HTML

More examples

Using the "beginbegin" value:

var head = document.getElementsByTagName("h2")[0];
head.insertAdjacentHTML("afterbegin", "<span style=""39;color:red;'>Hello world</span>
Test See‹/›

Using the "beforebegin" value:

var head = document.getElementsByTagName("h2")[0];
head.insertAdjacentHTML("beforebegin", "<span style="39;color:red;'>Hello world</span>
Test See‹/›

Using the "beforeend" value:

var head = document.getElementsByTagName("h2")[0];
head.insertAdjacentHTML("beforeend", "<span style="39;color:red;'>Hello world</span>
Test See‹/›

Related References

HTML DOM Reference:element.insertAdjacentElement() method

HTML DOM Reference:element.insertAdjacentText() method

HTML DOM Reference:node.insertBefore() method

HTML DOM Reference:node.appendChild() method

HTML DOM Element Object