HTMLCollection: length property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheHTMLCollection.length property returns the number ofitems in aHTMLCollection.
In this article
Value
An integer value representing the number of items in aHTMLCollection.
Examples
Thelength property is often useful in DOM programming. It's often used totest the length of a list, to see if it exists at all. It's also commonly used as theiterator in afor loop, as in this example.
js
// All the elements with the class ".test" in the documentconst items = document.getElementsByClassName("test");// For each test item in the list,// append the entire element as a string of HTMLlet gross = "";for (let i = 0; i < items.length; i++) { gross += items[i].innerHTML;}// gross is now all the HTML for the test elementsSpecifications
| Specification |
|---|
| DOM> # ref-for-dom-htmlcollection-length①> |