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 way to get it:
var x = document.getElementsByTagName("P")["myElement"];
Syntax
HTMLCollection.namedItem(name)
Or:
HTMLCollection[name]
Parameter Description:
| Parameters | Description |
|---|---|
| name | Required. The value of the id or name attribute of the element you want to return. |
Return Value
Return Element 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 »
Related Articles
HTMLCollection: item() Method
HTMLCollection: length property
DOM HTMLCollection