The hasChildNodes()
method returns true
if the specified node has any childNodes, otherwise false.
hasChildNodes |
Yes | Yes | Yes | Yes | Yes |
node.hasChildNodes()
None
It returns a Boolean type value.
true if the node has childnodes, otherwise false
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: