HTML DOM insertAdjacentText() Method
THE WORLD'S LARGEST WEB DEVELOPER SITE

HTML DOM insertAdjacentText() Method

Element Object

Example

Insert text after the specified header element:

var h = document.getElementById("myH2");
h.insertAdjacentText("afterend", "My inserted text");
Try it Yourself »

Definition and Usage

The insertAdjacentText() method inserts a the specified element into a specified position.

Legal position values are:

"afterbegin"
"afterend"
"beforebegin"
"beforeend"


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method
insertAdjacentText() Yes Yes 48 Yes Yes

Syntax

node.insertAdjacentText(position, text)

Parameter Values

Parameter Type Description
position String Required. A position relative to the element.
Legal values:
"afterbegin" - After the beginning 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)
text String The text you want to insert


More Examples

Example

Using the "afterbegin" value:

var h = document.getElementById("myH2");
h.insertAdjacentText("afterbegin", "My inserted text");
Try it Yourself »

Example

Using the "beforebegin" value:

var h = document.getElementById("myH2");
h.insertAdjacentText("beforebegin", "My inserted text");
Try it Yourself »

Example

Using the "beforeend" value:

var h = document.getElementById("myH2");
h.insertAdjacentText("beforeend", "My inserted text");
Try it Yourself »

Element Object