Javascript Reference - HTML DOM length Property








The length property returns the number of nodes in a collection.

Browser Support

length Yes Yes Yes Yes Yes

Syntax

nodelist.length 

Return Value

A Number type value representing the number of nodes in the nodelist.





Example

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: