You are here: Reference > JavaScript > client-side > HTML DOM > methods > hasChildNodes

hasChildNodes method

Browser support:
Returns whether the current node has any child nodes or not.
The child nodes can be retrieved with the childNodes collection.


object.hasChildNodes ( );
You can find the related objects in the Supported by objects section below.

Return value:

Boolean. One of the following values:
false The current element has no children.
true The current element has at least one child.

Example HTML code 1:

This example illustrates the use of the hasChildNodes method:
    <script type="text/javascript">
        function HasChild (id) {
            var div = document.getElementById (id);
            var hasChilds = div.hasChildNodes ();

            if (hasChilds) {
                alert ("The element has at least one child.");
            else {
                alert ("The element has no children.");
    <button onclick="HasChild ('div_1')">Does the first division have any children?</button>
    <button onclick="HasChild ('div_2')">Does the second division have any children?</button>
    <br /><br />
    First division:
    <div id="div_1" style="width:300px;height:80px; background-color:#e0d0a0;"><!-- comment inside the division --></div>
    Second division:
    <div id="div_2" style="width:300px;height:80px; background-color:#d0e0a0;"></div>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content