Open In App
Related Articles

HTML DOM children Property

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The DOM children property is used to return an HTML collection of all the child elements of the specified element. The elements in the collection can be accessed by index numbers. It differs from childNodes as childNodes contain all nodes (i.e. it includes text and comment nodes as well) but on the other hand, children contain only element nodes. This is a read-only property. 

Syntax:

element.children

Return Value: It returns a collection of element nodes that can be accessed by indexing. 

Example 1: This example returns the number of list items. 

HTML

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM children Property
    </title>
 
    <!-- Script to count children of parent attribute -->
    <script>
        function Geeks() {
            let count =
                document.getElementById("parent").children.length;
                document.getElementById("p").innerHTML =
                    "No of Children: " + count;
        }
    </script>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>
        DOM children Property
    </h2>
    <p>Searching Algorithms</p>
    <ul id="parent">
        <li>Merge sort</li>
        <li>Quick sort</li>
    </ul>
    <button onclick="Geeks()">
        Click Here!
    </button>
    <p id="p"></p>
</body>
</html>

                    

Output: 

 

Example 2: In this example, we will use  DOM children property

HTML

<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM children Property
    </title>
 
    <script>
        function Geeks() {
            let doc =
                document.getElementById("parent").children;
 
            let i;
            for (i = 0; i < doc.length; i++) {
                doc[i].style.color = "white";
                doc[i].style.backgroundColor = "green";
            }
        }
    </script>
</head>
 
<body style="text-align:center">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h2>
        DOM children Property
    </h2>
    <div id="parent">
        <p>
            A computer science portal for geeks.
        </p>
        <p>
            Geeks classes an extensive programme for geeks.
        </p>
    </div>
    <button onclick="Geeks()">Click me!</button>
</body>
</html>

                    

Output: 

 

Supported Browsers: The browser supported by DOM children property is listed below:

  • Google Chrome 1.0 and above
  • Edge 12.0 and above
  • Internet Explorer 9.0 and above
  • Firefox 3.5 and above
  • Opera 10.0 and above
  • Apple Safari 4.0 and above


Last Updated : 09 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads