HTMLCollection length property
- Previous Page item()
- Next Page namedItem()
- Go to the Previous Level DOM HTMLCollection
Definition and usage
length
The property returns the number of elements in the HTMLCollection.
This property is read-only.
This element is very useful when you want to iterate over an HTMLCollection.
Instance
Example 1
Find out how many P elements are in the document:
function myFunction() { var l = document.getElementsByTagName("P").length; alert(l); }
Example 2
Loop through all elements with class="myclass" and change their background color:
var x = document.getElementsByClassName("myclass"); for (i = 0; i < x.length; i++) { x[i].style.backgroundColor = "red"; }
Syntax
HTMLCollection.length
Return value
A number, representing the number of elements in the HTMLCollection.
Browser support
Properties | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
length | Supported | Supported | Supported | Supported | Supported |
Related pages
HTMLCollection:The item() method
HTML Element:The getElementsByClassName() method
HTML Element:getElementsByTagName() Method
- Previous Page item()
- Next Page namedItem()
- Go to the Previous Level DOM HTMLCollection