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 HTMLCollection
document.forms.address // A named element of an HTMLCollection

Properties

readonly unsigned long length

The number of elements in the collection.

Methods

Element 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)

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.

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.