We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

Nossos voluntários ainda não traduziram este artigo para o Português (do Brasil). Junte-se a nós e ajude-nos a fazer o trabalho!
Você também pode ler o artigo em English (US).

The insertAdjacentElement() method inserts a given element node at a given position relative to the element it is invoked upon.


targetElement.insertAdjacentElement(position, element);


A DOMString representing the position relative to the targetElement; must match (case-insensitively) one of the following strings:
  • 'beforebegin': Before the element itself.
  • 'afterbegin': Just inside the element, before its first child.
  • 'beforeend': Just inside the element, after its last child.
  • 'afterend': After the element itself.
The element to be inserted into the tree.

Return value

The element that was inserted, or null, if the insertion failed.


Exception Explanation
SyntaxError The position specified is not a recognised value.
TypeError The element specified is not a valid element.

Visualization of position names

<!-- beforebegin -->
<!-- afterbegin -->
<!-- beforeend -->
<!-- afterend -->
Note: The beforebegin and afterend positions work only if the node is in a tree and has an element parent.


beforeBtn.addEventListener('click', function() {
  var tempDiv = document.createElement('div');
  tempDiv.style.backgroundColor = randomColor();
  if (activeElem) {

afterBtn.addEventListener('click', function() {
  var tempDiv = document.createElement('div');
  tempDiv.style.backgroundColor = randomColor();
  if (activeElem) {

Have a look at our insertAdjacentElement.html demo on Github (see the source code too.) Here we have a sequence of <div> elements inside a container. When one is clicked, it becomes selected and you can then press the Insert before and Insert after buttons to insert new divs before or after the selected element using insertAdjacentElement().


Specification Status Comment
The definition of 'insertAdjacentElement()' in that specification.
Living Standard  

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) 48.0 (48.0) 8 (Yes) (Yes)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support ? 48.0 (48.0) ? ? ?

See also

Etiquetas do documento e colaboradores

 Colaboradores desta página: chrisdavidmills, deltab., trusktr, fscholz, Rob W, crei0
 Última atualização por: chrisdavidmills,