HTMLAllCollection
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see thecompatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
TheHTMLAllCollection interface represents a collection ofall of the document's elements, accessible by index (like an array) and by the element'sid. It is returned by thedocument.all property.
HTMLAllCollection has a very similar shape toHTMLCollection, but there are many subtle behavior differences — for example,HTMLAllCollection can be called as a function, and itsitem() method can be called with a string representing an element'sid orname attribute.
In this article
Instance properties
HTMLAllCollection.lengthRead onlyReturns the number of items in the collection.
Instance methods
HTMLAllCollection.item()Returns the element located at the specified offset into the collection, or the element with the specified value for its
idornameattribute. Returnsnullif no element is found.HTMLAllCollection.namedItem()Returns the firstelement in the collection whose
idornameattribute match the given string name, ornullif no element matches.
Usage in JavaScript
>Indexed access
In addition to the methods above, elements in anHTMLAllCollection can be accessed by integer indices and string property names. The HTMLid attribute may contain: and. as valid characters, which would necessitate using bracket notation for property access.collection[i] is equivalent tocollection.item(i), wherei can be an integer, a string containing an integer, or a string representing anid.
Calling as a function
AnHTMLAllCollection object is callable. When it's called with no arguments or withundefined, it returnsnull. Otherwise, it returns the same value as theitem() method when given the same arguments.
Special type conversion behavior
For historical reasons,document.all is an object that in the following ways behaves likeundefined:
- It isloosely equal to
undefinedandnull. - It isfalsy in boolean contexts.
- Its
typeofis"undefined".
These special behaviors ensure that code like:
if (document.all) { // Assume that we are in IE; provide special logic}// Assume that we are in a modern browserWill continue to provide modern behavior even if the code is run in a browser that implementsdocument.all for compatibility reasons.
However, in all other contexts,document.all remains an object. For example:
- It is notstrictly equal to either
undefinedornull. - When used on the left-hand side of thenullish coalescing operator (
??) or theoptional chaining operator (?.), it will not cause the expression to short-circuit.
Specifications
| Specification |
|---|
| HTML> # the-htmlallcollection-interface> |