firstChild
Summary
Gets a reference to the first child node in the childNodes collection of the object.
If the node is childless, null is returned.
Syntax
var result = element.firstChild;
element.firstChild = value;
Return Value
Returns an object of type ElementElement
Examples
The following code example implements the firstChild attribute to get the first child element of an object.
<body>
<ul id ="oList">
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ul>
<script type="text/javascript">
var oFirstChild = oList.firstChild;
</script>
</body>
The following example shows how the firstChild method will include white space nodes #text.
<p id="para-01">
First span
</p>
<p id="p2">first span, no whitespace</p>
<script type="text/javascript">
var p01 = document.getElementById('para-01');
alert(p01.firstChild.nodeName);// #text
var p2=document.getElementById('p2');
alert(p2.firstChild.nodeName);// SPAN
</script>
Syntax
Standards information
- Document Object Model (DOM) Level 1 Specification, Section 1.2
Attributions
Mozilla Developer Network : [Node.firstChild Article]
Microsoft Developer Network: [firstChild Property Article]