HTMLCollection namedItem()
Example
Get the content of the p element with id="myElement":
const element = collection.namedItem("myElement");
let text = element.innerHTML;
This shorthand produces the same result:
const element = collection["myElement"];
let text = element.innerHTML;
Description
The namedItem()
method returns a named element from
an HTMLCollection.
The namedItem()
method uses the id or name attribute to identify the element.
HTMLCollection
An HTMLCollection is an array-like collection (list) of HTML elements.
The length Property returns the number of elements in the collection.
The elements can be accessed by index (starts at 0).
An HTMLCollection is live. It is automatically updated when the document is changed.
See Also:
Syntax
Parameters
The value of the id attribute, or the name attribute, of the element.
Return Value
Browser Support
HTMLCollection.namedItem()
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |