HTMLCollection namedItem()Method
Definition and Usage
The namedItem() method returns the element with the specified ID or name in the HTMLCollection object.
You can also use the following shorthand to get it:
var x = document.getElementsByTagName("P")["myElement"];
Syntax
HTMLCollection.namedItem(name)
Or:
HTMLCollection[name]
Parameter Description:
| Parameter | Description |
|---|---|
| name | Required. The value of the id or name attribute of the element you want to return. |
Return Value
ReturnsElementAn object representing the element with the specified ID or name, or null if the element does not exist.
Browser Support
| Method | |||||
|---|---|---|---|---|---|
| namedItem() | Yes | Yes | Yes | Yes | Yes |
Example
Example
Get the element with ID "myElement" in the p element:
function myFunction() {
var x = document.getElementsByTagName("P").namedItem("myElement");
alert(x.innerHTML);
}
Try it Yourself »
Related Articles
HTMLCollection: item()Method
HTMLCollection: lengthProperty
Other Extensions
DOM HTMLCollection