Element.insertAdjacentElement() is an instance method that inserts an Element node at one of four positions relative to the target element: beforebegin, afterbegin, beforeend, and afterend. Learn the MDN position diagram, comparison with append() and insertAdjacentHTML(), and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
Element|null
03
Args
position + el
04
Inside
begin/end
05
Outside
before/after
06
Status
Baseline widely
Fundamentals
Introduction
When you build dynamic UIs, you often need to place a new element in a precise spot: before a list item, as the first child of a panel, at the end of a container, or right after a card. insertAdjacentElement(position, element) does exactly that without manually calling parent.insertBefore().
MDN: the method inserts a given element node at a given position relative to the element it is invoked upon. You pass a position string and an Element to insert.
💡
Beginner tip
Think of four slots around an element. afterbegin and beforeend go inside the element (first/last child). beforebegin and afterend go outside as siblings.
Element.insertAdjacentElement() is Baseline Widely available (MDN: across browsers since April 2018). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.insertAdjacentElement()
Insert an element at beforebegin, afterbegin, beforeend, or afterend — returns the node or null.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerNot supported
No
insertAdjacentElement()Excellent
Bottom line: Use insertAdjacentElement() when you need precise placement of Element nodes. For HTML strings, use insertAdjacentHTML().
Wrap Up
Conclusion
Element.insertAdjacentElement() inserts an element node at one of four positions around a target element. It is ideal when you need precise DOM placement without parsing HTML strings.
Use the MDN four-position diagram to pick the right slot
Create elements with createElement before inserting
Check the return value when insertion might fail
Prefer this over HTML strings when building nodes in JS
Use beforeend for append-like behavior inside a container
❌ Don’t
Pass HTML strings (use insertAdjacentHTML instead)
Use invalid position spellings (throws SyntaxError)
Assume beforebegin works without a parent element
Forget that moving an existing node removes it from its old parent
Confuse afterbegin with beforebegin
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.insertAdjacentElement()
Precise element insertion positions.
5
Core concepts
📝01
Returns
Element|null
API
📄02
Args
pos + el
Two
📄03
Inside
begin/end
Child
✅04
Outside
sibling
Peer
⚡05
Null
on fail
Check
❓ 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.
Did you know?
MDN: beforebegin and afterend insert outside the element as siblings, while afterbegin and beforeend insert as the first or last child inside the element.