Expand Minimize

appendChild method

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
acronym
address
article
aside
attribute
b
bdo
big
blockQuote
body
button
caption
center
cite
code
col
colGroup
comment
dd
del
dfn
dir
div
dl
documentType
dt
em
fieldSet
figcaption
figure
font
footer
form
frameSet
head
header
hgroup
hn
html
i
iframe
input type=button
input type=checkbox
input type=email
input type=file
input type=image
input type=number
input type=password
input type=radio
input type=range
input type=reset
input type=search
input type=submit
input type=tel
input type=text
input type=url
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

 

 

Show:
© 2014 Microsoft