Name
HTMLCollection: array of HTML elements accessible by position or name â DOM Level 2 HTML: Object â HTMLCollection
Properties
-
readonly unsigned long length
The number of elements in the collection.
Methods
-
item( )
Returns the element at the specified position in the collection. You can also simply specify the position within array brackets instead of calling this method explicitly.
-
namedItem( )
Returns the element from the collection that has the specified value for its
id
orname
attribute, ornull
if there is no such element. You can also place the element name within array brackets instead of calling this method explicitly.
Description
An HTMLCollection is a collection of HTML elements with
methods that allow you to retrieve an element by its position in the
collection or by its id
or
name
attribute. In JavaScript,
HTMLCollection objects behave like read-only arrays, and you can use
JavaScript square-bracket notation to index an HTMLCollection by
number or by name instead of calling the item( )
and namedItem( )
methods.
A number of the properties of the HTMLDocument object are
HTMLCollection objects and provide convenient access to document
elements such as forms, images, and links. The Form.elements
property and Select.options
property are HTMLCollection
objects. The HTMLCollection object also provides a convenient way to traverse the rows
of a Table and the cells of a TableRow.
HTMLCollection objects are read-only: you cannot assign new elements to them, even when using JavaScript ...
Get JavaScript: The Definitive Guide, 5th 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.