目录

HTML DOM Element insertAdjacentText()

Example

Insert text after a header element:

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

Description

The insertAdjacentText() method inserts a a text into a specified position.

Legal positions:

Value Description
afterbegin After the beginning of the element (first child)
afterend After the element
beforebegin Before the element
beforeend Before the end of the element (last child)

Syntax

element.insertAdjacentText( position, text)
node.insertAdjacentText( position, text)

Parameters

Parameter Description
position Required.
A position relative to the element:
afterbegin
afterend
beforebegin
beforeend
text The text to insert.


More Examples

Example

Using "afterbegin":

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

Example

Using "beforebegin":

h2.insertAdjacentText("beforebegin", text);
Try it Yourself »

Example

Using "beforeend":

h2.insertAdjacentText("beforeend", text);
Try it Yourself »

Browser Support

element.insertAjacentText() is supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes