HTMLCollection extension type
The HTMLCollection
interface represents a generic collection
(array-like object similar to arguments
) of elements (in document order)
and offers methods and properties for selecting from the list.
An HTMLCollection
in the HTML DOM is live; it is automatically updated
when the underlying document is changed. For this reason it is a good idea
to make a copy (e.g., using Array.from
) to iterate over if adding, moving,
or removing nodes.
This interface is called HTMLCollection
for historical reasons, because
before the modern DOM, collections implementing this interface could only
have HTML elements as their items.
This interface was an attempt to create an unmodifiable list and only continues to be supported to not break code that's already using it. Modern APIs represent list structures using types based on JavaScript arrays, thus making many array methods available, and at the same time imposing additional semantics on their usage (such as making their items read-only).
These historical reasons do not mean that you as a developer should avoid
HTMLCollection
. You don't create HTMLCollection
objects yourself, but
you get them from APIs such as Document.getElementsByClassName, and these
APIs are not deprecated. However, be careful of the semantic differences
from a real array.
API documentation sourced from MDN Web Docs.
- on
- Implemented types
- Available extensions
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- length → int
-
The
HTMLCollection.length
property returns the number of items in a HTMLCollection.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
item(
int index) → Element? -
The HTMLCollection method
item()
returns the element located at the specified offset into the collection. -
namedItem(
String name) → Element? -
The
namedItem()
method of the HTMLCollection interface returns the first Element in the collection whoseid
orname
attribute match the specified name, ornull
if no element matches. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited