Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM appendChild() method

HTML DOM appendChild() method

Element Object Reference Element object

Example

Add a list item:

document.getElementById("myList").appendChild(

newListItem

);

Before adding:

After adding:

Try it »


Definition and Usage

The appendChild() method adds a new child node to the end of the list of child nodes of a node.

**Note:** If newchild already exists in the document tree, it will be removed from the document tree and then re-inserted at its new position. If newchild is a DocumentFragment node, it will not be inserted directly; instead, its child nodes are inserted in order at the end of the current node's childNodes[] array.

You can use the appendChild() method to move an element to another element.

Example

Move a list item to another list item:

var node=document.getElementById("myList2").lastChild;

document.getElementById("myList1").appendChild(node);

Before adding:

After adding:

Try it »


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the appendChild() method


Syntax

node

.appendChild(

node

)

Parameters

Parameters Type Description
node Node object Required. The node object you want to add.

Return Value

Type Description
Node object The added node

Technical Details

DOM version Core Level 1 Node Object

Element Object Reference Element object