The length
property returns the number of nodes in a collection.
length |
Yes | Yes | Yes | Yes | Yes |
nodelist.length
A Number type value representing the number of nodes in the nodelist.
The following code shows how to get the number of child nodes of the body element.
<!DOCTYPE html>
<html>
<body><p id="demo">this is a test.</p>
<button onclick="myFunction()">test</button>
<script>
function myFunction()<!-- w ww . j a v a 2 s . c om-->
{
var x=document.getElementById("demo");
x.innerHTML=document.body.childNodes.length;
}
</script>
</body>
</html>
The code above is rendered as follows: