appendChild method

This topic has not yet been rated - 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.

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
address
attribute
article
aside
b
bdo
blockQuote
body
button
caption
cite
code
col
colGroup
comment
dd
del
div
dl
dt
documentType
em
figcaption
figure
fieldSet
footer
form
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
map
mark
nav
ol
option
p
pre
ProcessingInstruction
q
s
samp
section
small
span
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
u
ul
var
Reference
cloneNode
insertBefore
Conceptual
About the W3C Document Object Model

 

 

Build date: 11/28/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.