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:

ValueDescription
afterbeginAfter the beginning of the element (first child)
afterendAfter the element
beforebeginBefore the element
beforeendBefore 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
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

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.