Rearranging objects

The methods that can be used for adding newly created nodes to the DOM tree structure can also be used to move existing nodes to other locations.

Name Meaning
.appendChild(item) adds item as the last child of parent
.insertBefore(item, previousChild) inserts item before the child previousChild of parent
.replaceChild(item, oldChild) replaces the child oldChild of parent with item

Since inserting a node in one place automatically removes it from its earlier location, the method .insertBefore(item, previousChild) can be used to rearrange siblingsA sibling of a node x is a node with the same parent as x..

Try to guess the result before clicking on the Load page button.

Note:

Hints:

  • undefined
1
2
var nodeOrdered = document.getElementById("ordered");
var nodeOne = document.getElementById("one");
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
<!DOCTYPE html>
<html lang="en">
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX