Pick afterbegin. First child inside the target (MDN).
Insert the Element. Existing content shifts after it.
Like prepend. Similar idea to prepend() for one element.
Works without a parent. Inside positions do not need siblings’ parent check.
Compatibility
Browser Support
Element.insertAdjacentElement() is Baseline Widely available (MDN: across browsers since April 2018). Safe for modern teaching and production code.
✓ Baseline · Widely available
insertAdjacentElement()
Insert an Element node at a position relative to another element.
WideBaseline
Google ChromeFull support
Supported
Mozilla FirefoxFull support
Supported
Apple SafariFull support
Supported
Microsoft EdgeFull support
Supported
OperaFull support
Supported
Internet Explorer8+
Supported
insertAdjacentElement()Widely available
Bottom line: Use insertAdjacentElement when you already have an Element node and a clear relative position.
Frequently Asked Questions
It inserts a given element node at a position relative to the element it is called on. MDN positions: beforebegin, afterbegin, beforeend, afterend.
No. MDN marks Element.insertAdjacentElement() as Baseline Widely available (across browsers since April 2018). It is not Deprecated, Experimental, or Non-standard.
beforebegin (before the element), afterbegin (first child inside), beforeend (last child inside), afterend (after the element). Matching is case-insensitive (MDN).
The element that was inserted, or null if insertion failed (MDN).
insertAdjacentElement() takes an Element node. insertAdjacentHTML() parses an HTML string into nodes.
MDN: they work only when the node is in a tree and has an element parent.