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

obj.length;

Return Value

A Number representing the number of nodes in the nodemap.





Example

The following code shows how to get the number of attributes of a button element.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from  www  . ja  v  a2  s.  co m-->
<p id="demo"></p>

<script>
function myFunction() {
    var a = document.getElementsByTagName("BUTTON")[0].attributes;
    var x = document.getElementById("demo");  
    x.innerHTML = a.length;
}
</script>
</body>
</html>

The code above is rendered as follows: