HTML | DOM childNodes Property

The childNodes property returns a node’s child nodes as a nodeList object. White spaces and comments are also considered as nodes. The nodes are assigned index numbers, starting from 0. Searching and sorting operations can be performed using index number on the node list.

Syntax:

elementNodeReference.childNodes;

Return Value: It returns a collection of child nodes of a particular node as a nodeList object (including white spaces, texts, and comments, which are considered as nodes).

Properties:

  1. length property: It determines the number of child nodes of the object. It is a read only property.
    Syntax:

    elementNodeReference.childNodes.length;
    elementNodeReference.childNodes[index_number].length;

    Example-1: Showing length property.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <body>
        <h1><center>Geeks
        <button onclick="node()">Press</button>
       </center> </h1>
      
            <h4>Clicking on the 'Press' button will return 
              the length of childNodes[0].</h4>
      
        <p id="gfg"></p>
      
        <script>
            function node() {
                
                // Return the length of child node.
                var c = document.getElementsByTagName(
                  "BUTTON")[0];
                var x = c.childNodes[0].length;
                document.getElementById("gfg").innerHTML = x;
            }
        </script>
      
    </body>
      
    </html>

    chevron_right

    
    

    Output:

    Before clicking on the button:

    After clicking on the button:

  2. nodeName property: It returns the name of the specified node. If the node is an element node, it will return the tag name else if the node is an attribute node, it will return the attribute name else for different node types, different names will be returned.

    Syntax:

    elementNodeReference.childNodes[index_number].nodeName;

    Example-2: Showing nodeName property

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <body>
        <h1><center>Geeks 
          <button onclick="node()">Press
          </button></center> </h1>
      
            <h4>Clicking on the 'Press' button will 
              return the node name of childNodes[0].</h4>
      
        <p id="gfg"></p>
      
        <script>
            function node() {
                
                //  Return the name of specific node name.
                var c = document.getElementsByTagName(
                  "BUTTON")[0];
                var x = c.childNodes[0].nodeName;
                document.getElementById("gfg").innerHTML = x;
            }
        </script>
      
    </body>
      
    </html>

    chevron_right

    
    

    Output:

    Before clicking on the button:

    After clicking on the button:

  3. nodeValue property: It sets or returns the node value of the specified node.

    Syntax:

    elementNodeReference.childNodes[index_number].nodeValue;

    Example-3: Showing nodeValue property

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <body>
        <h1><center>Geeks 
          <button onclick="node()">Press
          </button></center> </h1>
      
            <h4>Clicking on the 'Press' button will 
              return the node value of childNodes[0].</h4>
      
        <p id="gfg"></p>
      
        <script>
            function node() {
                
                
                // Return the node value.
                var c = document.getElementsByTagName("BUTTON")[0];
                var x = c.childNodes[0].nodeValue;
                document.getElementById("gfg").innerHTML = x;
            }
        </script>
      
    </body>
      
    </html>

    chevron_right

    
    

    Output:

    Before clicking on the button:

    After clicking on the button:

Browser Support: The listed browsers support DOM childNodes property:

  • Google Chrome
  • Firefox
  • Internet Explorer
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.