HTML DOM Element insertAdjacentText() Method

Definition and Usage

insertAdjacentText() The method inserts text at the specified position.

Valid positions:

Value Description
afterbegin After the element starts (the first child element).
afterend After the element.
beforebegin Before the element.
beforeend Before the element ends (the last child element).

Example

Example 1

Insert text after the title element:

const h2 = document.getElementById("myH2");
let text = "My inserted text";
h2.insertAdjacentText("afterend", text);

Try it yourself

Example 2

Using "afterbegin":

const h2 = document.getElementById("myH2");
let text = "My inserted text";
h2.insertAdjacentText("afterbegin", text);

Try it yourself

Example 3

Using "beforebegin":

h2.insertAdjacentText("beforebegin", text);

Try it yourself

Example 4

Using "beforeend":

h2.insertAdjacentText("beforeend", text);

Try it yourself

Syntax

element.insertAdjacentText(position, text)

or

node.insertAdjacentText(position, text)

Parameter

Parameter Description
position

Required. The position relative to the element:

  • afterbegin
  • afterend
  • beforebegin
  • beforeend
text The text to be inserted.

Browser support

All browsers support element.insertAdjacentText()

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support