Node firstChild Property - Javascript DOM

Javascript examples for DOM:Node

Description

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

firstChild returns the first child node as an element node, a text node or a comment node.

firstElementChild returns the first child node as an element node ignoring text and comment nodes.

Whitespace inside elements is considered as text, which is considered as nodes.

This property is read-only.

Return Value

A Node object, representing the first child of a node, or null if there are no child nodes

The following code shows how to get the HTML content of the first child node of an <ul> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<select id="mySelect" size="4">
    <option>Java</option>
    <option>SQL</option>
    <option>HTML</option>
    <option>CSS</option><
/select>
<br><br>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//w w  w  .ja  v  a  2 s  .co m
    var x = document.getElementById("mySelect").firstChild.text;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials