Element childNodes Property - Javascript DOM

Javascript examples for DOM:Element childNodes

Description

The childNodes property returns a collection of a node's child nodes, as a NodeList object.

The nodes in the collection are sorted as they appear in the HTML and can be accessed by index numbers starting at 0.

text is considered as nodes and Comments are also considered as nodes.

This property is read-only.

Return Value

A NodeList object, representing a collection of nodes.

The following code shows how to get a collection of the <body> element's child nodes:

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() {/*from  w  w  w . j a  va  2 s.c  om*/
    var c = document.getElementById("mySelect").childNodes;
    document.getElementById("demo").innerHTML = c[2].text;
}
</script>

</body>
</html>

Related Tutorials