Web/API/Element/insertAdjacentElement

From Get docs

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

Syntax

targetElement.insertAdjacentElement(position, element);

Parameters

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

Return value

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

Exceptions

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 -->
<p>
  <!-- afterbegin -->
  foo
  <!-- beforeend -->
</p>
<!-- afterend -->

Note: The beforebegin and afterend positions work only if the node is in a tree and has an element parent.

Example

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

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

Have a look at our [[../../../../../../../mdn.github.io/dom-examples/insert-adjacent/insertAdjacentElement|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

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

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
insertAdjacentElement Chrome

Full support 1

Edge Full support 17


Full support 17


No support 12 — 17

Notes'

Notes' This function is implemented in the HTMLElement API, meaning non-HTML elements (like SVG elements) cannot use this function.

Firefox

Full support 48

IE Full support 8

Notes'

Full support 8

Notes'

Notes' This function is implemented in the HTMLElement API, meaning non-HTML elements (like SVG elements) cannot use this function.

Opera

Full support 8

Safari

Full support 3

WebView Android

Full support 1

Chrome Android

Full support 18

Firefox Android

Full support 48

Opera Android

Full support 10.1

Safari iOS

Full support 1

Samsung Internet Android

Full support 1.0

Legend

Full support  
Full support
See implementation notes.'
See implementation notes.


See also