This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

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.

Property of dom/Nodedom/Node

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

Attributions