Element.moveBefore() is an instance method that moves a node inside the caller as a direct child, before a reference node. Unlike insertBefore(), it preserves focus, animations, popover state, and more (MDN). Learn the MDN toggle demo, null reference append, constraints, feature detection, and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Moves
Element or text
03
Args
movedNode + ref
04
Returns
undefined
05
Preserves
focus, state
06
Status
Limited availability
Fundamentals
Introduction
When you rearrange UI with insertBefore() or prepend(), the browser removes the node and inserts it again. That can reset focus, close popovers, interrupt CSS transitions, and disturb other element state.
moveBefore(movedNode, referenceNode) moves a node to a new position without that remove-and-reinsert cycle (MDN). The node keeps its interactive and visual state—a big win for draggable panels, live video embeds, and modals.
💡
Beginner tip
Pass null as referenceNode to append movedNode at the end of the parent’s children (MDN).
Calling parent.moveBefore(movedNode, referenceNode) relocates movedNode inside parent, immediately before referenceNode.
It is an instance method on Element (MDN).
movedNode must be an Element or CharacterData node.
referenceNode must be a child of the caller, or null to append.
Returns undefined (MDN).
Preserves animation, focus, popover, modal, and loading state (MDN).
Only works within the same document; connected/disconnected mismatches throw (MDN).
MutationObserver records a removed node and an added node for the move (MDN).
Foundation
📝 Syntax
General form of Element.moveBefore (MDN):
JavaScript
moveBefore(movedNode, referenceNode)
Parameters
Name
Type
Description
movedNode
Node
The node to move. Must be an Element or CharacterData node (MDN).
referenceNode
Node or null
The child before which movedNode is placed, or null to append at the end of the parent’s children (MDN).
Return value
Type
Description
undefined
None (undefined) — the method updates the DOM in place (MDN).
Exceptions
HierarchyRequestError — invalid moves such as cross-document, disconnected nodes, moving an ancestor into a descendant, invalid referenceNode, missing second argument, or wrong node type (MDN).
Common patterns
JavaScript
// MDN toggle: move before section1 or section2
if (mover.nextElementSibling === section1) {
wrapper.moveBefore(mover, section2);
} else {
wrapper.moveBefore(mover, section1);
}
// Append to end of a container
section2.moveBefore(mover, null);
// Feature-detect with insertBefore fallback
function moveNode(parent, node, ref) {
if (typeof parent.moveBefore === "function") {
parent.moveBefore(node, ref);
} else {
parent.insertBefore(node, ref);
}
}
Cheat Sheet
⚡ Quick Reference
Goal
Code
Move before a child
parent.moveBefore(node, ref)
Append at end
parent.moveBefore(node, null)
MDN toggle pattern
wrapper.moveBefore(mover, section2)
Feature detect
typeof el.moveBefore === "function"
Return value
undefined
MDN status
Limited availability (not Baseline)
Snapshot
🔍 At a Glance
Four facts to remember about Element.moveBefore().
Returns
void
undefined
Status
limited
Not Baseline
Preserves
state
focus, popovers
null ref
append
End of children
Compare
📋 moveBefore() vs insertBefore() vs prepend()
moveBefore()
insertBefore()
prepend()
Mechanism
Move in place
Remove + reinsert
Remove + reinsert
Preserves state
Yes (MDN)
Often resets
Often resets
Return value
undefined
Moved node
undefined
Browser support
Limited
Universal
Baseline
Best for
Stateful UI moves
Fallback / legacy
Simple prepend
Hands-On
Examples Gallery
Examples follow MDN Element.moveBefore() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
MDN’s basic toggle—move a box between two sections.
Example 1 — MDN Toggle Between Sections
Click a button to move #mover before #section1 or #section2.
Element.moveBefore() has limited availability on MDN (not Baseline). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Experimental · Not Baseline
Element.moveBefore()
Move nodes while preserving focus and UI state — limited cross-browser support.
LimitedNot Baseline
Google ChromeSupported · Recent versions
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxCheck current MDN table
Partial
Apple SafariCheck current MDN table
Partial
OperaChromium-based versions
Yes
Internet ExplorerNot supported
No
moveBefore()Growing
Bottom line: Feature-detect moveBefore() and fall back to insertBefore() where needed. Use it when preserving element state during DOM moves matters.
Wrap Up
Conclusion
Element.moveBefore() relocates a node inside a parent while preserving interactive and visual state—something insertBefore() often disrupts. Pass null to append, feature-detect for limited browsers, and fall back when needed.
Fall back to insertBefore() when unsupported (MDN)
Use null reference to append at end (MDN)
Prefer for stateful moves: focus, popovers, animations
Wrap risky moves in try/catch for HierarchyRequestError
❌ Don’t
Assume Baseline support—check MDN compatibility first
Move nodes across different documents
Move disconnected nodes into connected parents (or vice versa)
Move an ancestor into its own descendant
Omit the second referenceNode argument (throws per MDN)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.moveBefore()
Move nodes without losing UI state.
5
Core concepts
📝01
Returns
undefined
void
📄02
Preserves
focus
State
✍️03
null ref
append
MDN
✅04
Status
limited
Detect
⚡05
vs
insertBefore
Fallback
❓ Frequently Asked Questions
It moves a given Node inside the invoking element as a direct child, before a reference node (MDN). Unlike insertBefore(), it does not remove and reinsert the node, so element state is preserved.
MDN marks Element.moveBefore() as limited availability (not Baseline). It is a newer DOM API with limited cross-browser support. Feature-detect before using it in production.
Nothing useful — the return value is undefined (MDN).
If referenceNode is null, movedNode is inserted at the end of the invoking element's child nodes (MDN).
insertBefore() removes and reinserts the node, which can reset focus, animations, and popover state. moveBefore() moves the node while preserving that state (MDN).
HierarchyRequestError for invalid moves (wrong document, disconnected nodes, ancestor moves, invalid referenceNode, missing second argument, or non-Element/CharacterData movedNode) per MDN.
Did you know?
MDN’s YouTube embed demo shows a key difference: moving with insertBefore() or prepend() resets playback, but moveBefore() keeps the video playing.