HTMLCollection namedItem()
Example
Get the content of the p element with id="myElement":
const collection = document.getElementsByTagName("p");
const element = collection.namedItem("myElement");
let text = element.innerHTML;
Try it Yourself »
This shorthand produces the same result:
const collection = document.getElementsByTagName("p");
const element = collection["myElement"];
let text = element.innerHTML;
Try it Yourself »
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
HTMLCollection.namedItem(name)
or
HTMLCollection[name]
Parameters
Parameter | Description |
name | Required. The value of the id attribute, or the name attribute, of the element. |
Return Value
Type | Description |
Element | The element with the specified id or name.null if the element does not exist. |
Browser Support
HTMLCollection.namedItem()
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |