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

HTML DOM insertAdjacentText() method

HTML DOM Element Object

insertAdjacentText()The method inserts a given text node at a position relative to the element being called.

Syntax:

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

Browser compatibility

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

Method
insertAdjacentText()Is48IsIs8

Parameter value

ParameterDescription
positionPosition relative to the element.
Valid values:
  • "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)

textThe text you want to insert

Technical details

Return value:None
Exception cases:SyntaxError-SpecifiedPosition (Location)Not a recognizable value

More examples

Using the "beginbegin" value:

var head = document.getElementsByTagName("h2")[0];
head.insertAdjacentText("afterbegin", "Hello world");
Test and See‹/›

Using the "beforebegin" value:

var head = document.getElementsByTagName("h2")[0];
head.insertAdjacentText("beforebegin", "Hello world");
Test and See‹/›

Using the "beforeend" value:

var head = document.getElementsByTagName("h2")[0];
head.insertAdjacentText("beforeend", "Hello world");
Test and See‹/›

Related References

HTML DOM Reference:element.insertAdjacentElement() method

HTML DOM Reference:element.insertAdjacentHTML() method

HTML DOM Reference:node.insertBefore() method

HTML DOM Reference:node.appendChild() method

HTML DOM Element Object