Javascript Reference - HTML DOM firstChild Property








The firstChild property returns the first child node of the specified node, as a Node object.

Browser Support

firstChild Yes Yes Yes Yes Yes

Syntax

node.firstChild

Return Value

The first child of the node, as a Node object.





Example

The following code shows how to get the first child node of the document.


<!DOCTYPE html>
<html>
<body>
<p id="demo">test</p>
<!--  www .j a  v a 2 s.  co m-->
<button onclick="myFunction()">test</button>

<script>
function myFunction()
{
    var x=document.getElementById("demo");  
    x.innerHTML=document.firstChild.nodeName;
}
</script>

</body>
</html>

The code above is rendered as follows: