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 »
Definition and Usage
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 elements in a collection can be accessed by index (starts at 0).
The length Property returns the number of elements in the collection.
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 | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |