Name
HTMLCollection — an element collection accessible by name or number
Synopsis
An HTMLCollection is a read-only array-like object of Element objects that also defines properties
corresponding to the name and
id values of the collected
elements. The Document object defines HTMLCollection properties such
as forms and image.
HTMLCollection objects define item() and namedItem() methods, for retrieving
elements by position or name, but it is never necessary to use them:
you can simply treat the HTMLCollection as a JavaScript object and
access its properties and array elements. For example:
document.images[0]// A numbered element of an HTMLCollectiondocument.forms.address// A named element of an HTMLCollection
Properties
readonly unsigned longlengthThe number of elements in the collection.
Methods
Element item(unsigned long
index)
item(unsigned long
index)Returns the element at the specified
index in the collection or null if index
is out of bounds. You can also simply specify the position within
array brackets instead of calling this method explicitly.
object namedItem(string
name)
namedItem(string
name)Returns the first element from the collection that has the
specified name for its id or name attribute, or null if there is no such element. You
can also place the element name within array brackets instead of
calling this method explicitly.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access