Javascript Reference - HTML DOM hasChildNodes Method








The hasChildNodes() method returns true if the specified node has any childNodes, otherwise false.

Browser Support

hasChildNodes Yes Yes Yes Yes Yes

Syntax

node.hasChildNodes()

Parameters

None

Return Value

It returns a Boolean type value.

true if the node has childnodes, otherwise false





Example

The following code shows how to see if an element has any child Nodes.


<!DOCTYPE html>
<html>
<body>
<ul id="myList"><li>A</li><li>B</li></ul>
<p id="demo">test</p>
<button onclick="myFunction()">test</button>
<script>
function myFunction()<!--from  ww  w . j av a 2  s  . c  o  m-->
{
    var lst=document.getElementById("myList");
    var x=document.getElementById("demo");
    x.innerHTML=lst.hasChildNodes();
}
</script>
</body>
</html>

The code above is rendered as follows: