Element: moveBefore() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The moveBefore() method of the Element interface moves a given Node inside the invoking node as a direct child, before a given reference node.

Syntax

js
moveBefore(movedNode, referenceNode)

Parameters

movedNode

A Node representing the node to be moved. Note that this must be an Element or a CharacterData node.

referenceNode

A Node that movedNode will be moved before, or null. If the value is null, movedNode is inserted at the end of the invoking node's child nodes.

Return value

None (undefined).

Exceptions

HierarchyRequestError TypeError

Thrown in any of the following situations:

  • The specified movedNode is not part of the DOM, and you are trying to move it inside a node that is part of the DOM, or vice versa.
  • The specified movedNode is an ancestor of the Element moveBefore() is being called on.
  • You are trying to move movedNode between two different documents.
  • The specified movedNode is not an Element or CharacterData node.
NotFoundError TypeError

The specified referenceNode is not a child of the node you are calling moveBefore() on, that is, the node you are trying to move movedNode inside.

TypeError TypeError

The second argument was not supplied.

Description

The moveBefore() method moves a given node to a new place in the DOM. It provides similar functionality to the Node.insertBefore() method, except that it doesn't remove and then reinsert the node. This means that the state of the node (which would be reset if moving it with insertBefore() and similar mechanisms) is preserved after the move. This includes:

Try clicking the