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.
Retrieves the number of immediate child nodes of the current element or a zero if the element does not contain any child nodes. childElementCount does not return all child nodes, only child nodes that are nodeType =1, or element nodes.
![]() |
Syntax
| JavaScript | |
|---|
Property values
Type: Integer
The number of child elements.
Standards information
- Element Traversal Specification, Section 2.5
Remarks
The childElementCount property only returns immediate children of the current node. It does not count descendent children of the immediate children.
Examples
This example shows how to use childElementCount to get the number of immediate children of a div tag. Be aware that because decendent children of the the div tag "divWithChildren" are ignored.
<!DOCTYPE html>
<html>
<head>
<title>childElementCount example</title>
<script>
function GetCount () {
var testArea = document.getElementById ("testArea");
var childCount = 0;
childCount = testArea.childElementCount;
alert ("The number of child elements is " + childCount);
}
</script>
</head>
<body>
<div id="testArea" >
<p>This is the test area, which contains several children.</p>
<div id="divWithChildren">
<div>a descendant child of a div</div>
<div>also a descendent child of a div</div>
</div>
<p>A paragraph tag to consider.</p>
<input type="text" size="80" value="And a text box as well"/>
</div>
<p><input type="button" value="Get the number child elements in our test" name="abutton" onclick="GetCount ();" /> </p>
</body>
</html>
See also
- a
- abbr
- acronym
- address
- applet
- area
- attribute
- b
- base
- baseFont
- bdo
- big
- blockQuote
- body
- button
- caption
- center
- cite
- code
- col
- colGroup
- comment
- dd
- del
- dfn
- dir
- div
- dl
- dt
- em
- embed
- fieldSet
- font
- form
- frame
- frameSet
- head
- hn
- hr
- html
- i
- iframe
- img
- 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
- link
- listing
- map
- marquee
- menu
- nextID
- ol
- option
- p
- plainText
- pre
- q
- s
- samp
- script
- select
- small
- span
- strike
- strong
- sub
- sup
- table
- tBody
- td
- textArea
- tFoot
- th
- tHead
- title
- tr
- tt
- u
- ul
- var
- xmp
Send comments about this topic to Microsoft
Build date: 11/8/2012
