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 »
let text = "My inserted text";
h2.insertAdjacentText("afterend", text);
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)
or
node.insertAdjacentText(position, text)
Parameters
Parameter
Description
position
Required.
A position relative to the element:
afterbegin
afterend
beforebegin
beforeend
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 »
let text = "My inserted text";
h2.insertAdjacentText("afterbegin", text);
Browser Support
element.insertAjacentText() is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera | IE |
| Yes | Yes | Yes | Yes | Yes | Yes |