HTML DOM Element insertAdjacentElement() Method
- Previous Page innerText
- Next Page insertAdjacentHTML()
- Go to the Previous Level HTML DOM Elements Object
Definition and Usage
insertAdjacentElement()
The method inserts the element 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 a span element after the title:
const span = document.getElementById("mySpan"); const h2 = document.getElementById("myH2"); h2.insertAdjacentElement("afterend", span);
Example 2
Using "afterbegin":
const span = document.getElementById("mySpan"); const h2 = document.getElementById("myH2"); h2.insertAdjacentElement("afterbegin", span);
Example 3
Using "beforebegin":
const span = document.getElementById("mySpan"); const h2 = document.getElementById("myH2"); h2.insertAdjacentElement("beforebegin", span);
Example 4
Using "beforeend":
const span = document.getElementById("mySpan"); const h2 = document.getElementById("myH2"); h2.insertAdjacentElement("beforeend", span);
Syntax
element.insertAdjacentElement(position, element)
or
node.insertAdjacentElement(position, element)
Parameter
Parameter | Description |
---|---|
position |
Required. The position relative to the element:
|
element | The element to be inserted. |
Browser support
All browsers support element.insertAdjacentElement()
:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
- Previous Page innerText
- Next Page insertAdjacentHTML()
- Go to the Previous Level HTML DOM Elements Object