appendChild method

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
25 out of 39 rated this helpful - Rate this topic

Appends an element as a child to the object.

Document Object Model (DOM) Level 3 Core Specification, Section 1.4

Syntax

object.appendChild(newChild)

Parameters

newChild [in]

Type: IHTMLDOMNode

Object that specifies the element to append.

Return value

Type: IHTMLDOMNode

Returns a reference to the element that is appended to the object.

Standards information

Remarks

The appendChild method appends elements to the end of the childNodes collection.

To display new elements on the page, you must append them within the body element. For example, the following syntax demonstrates how to add a div element to the body.


var oDiv=document.createElement("div");
document.body.appendChild(oDiv);

This method is accessible at run time. If elements are removed at run time, before the closing tag is parsed, areas of the document might not render.

Windows Internet Explorer 9. Exceptions are only supported when webpages are displayed in IE9 Standards mode.

In Microsoft Internet Explorer 6, This method now applies to the attribute object.

Examples

This example uses the appendChild method to add an item to an unordered list.


<!doctype html>
<html>
</head>
<title>Append child sample</title>
<script>
  function fnAppend() {
    var list = document.getElementById("mylist");
    var newnode = document.createElement("li");
    list.appendChild(newnode);
    newnode.innerText = "List node 5";
  }
</script>
</head>
<body>
<ul id = "mylist">
<li>List node 1</li>
<li>List node 2</li>
<li>List node 3</li>
<li>List node 4</li>
</ul>
<input
   type = "button"
   value = "Append child"
   onclick = "fnAppend()" />
</body>
</html>

See also

a
abbr
acronym
address
attribute
article
aside
b
bdo
big
blockQuote
body
button
caption
center
cite
code
col
colGroup
comment
dd
del
dfn
dir
div
dl
dt
documentType
em
figcaption
figure
fieldSet
font
footer
form
frameSet
head
header
hgroup
hn
html
i
iframe
input type=button
input type=checkbox
input type=file
input type=image
input type=password
input type=radio
input type=reset
input type=submit
input type=text
ins
kbd
label
legend
li
listing
map
mark
marquee
menu
nav
nextID
ol
option
p
plainText
pre
ProcessingInstruction
q
s
samp
section
small
span
strike
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
tt
u
ul
var
xmp
Reference
cloneNode
insertBefore
Conceptual
About the W3C Document Object Model

 

 

Send comments about this topic to Microsoft

Build date: 11/27/2012

Did you find this helpful?
(1500 characters remaining)

Community Additions

© 2013 Microsoft. All rights reserved.