HTML DOM Element insertAdjacentText() Method
- Previous Page insertAdjacentHTML()
- Next Page insertBefore()
- Go to the Previous Level HTML DOM Elements Object
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);
Example 2
Using "afterbegin":
const h2 = document.getElementById("myH2"); let text = "My inserted text"; h2.insertAdjacentText("afterbegin", text);
Example 3
Using "beforebegin":
h2.insertAdjacentText("beforebegin", text);
Example 4
Using "beforeend":
h2.insertAdjacentText("beforeend", text);
Syntax
element.insertAdjacentText(position, text)
or
node.insertAdjacentText(position, text)
Parameter
Parameter | Description |
---|---|
position |
Required. The position relative to the element:
|
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 |
- Previous Page insertAdjacentHTML()
- Next Page insertBefore()
- Go to the Previous Level HTML DOM Elements Object