HTMLCollection namedItem() Method
THE WORLD'S LARGEST WEB DEVELOPER SITE

HTMLCollection namedItem() Method

HTMLCollection

Example

Get the content of the P element with ID "myElement":

function myFunction() {
  var x = document.getElementsByTagName("P").namedItem("myElement");
  alert(x.innerHTML);
}
Try it Yourself »

Definition and Usage

The namedItem() method returns the element with the specified ID, or name, in an HTMLCollection.

A shorthand method can also be used, and will produce the same result:

var x = document.getElementsByTagName("P")["myElement"]; Try it

Browser Support

Method
namedItem() Yes Yes Yes Yes Yes

Syntax

HTMLCollection.namedItem(name)
OR:
HTMLCollection[name]

Parameter Values

Parameter Description
name Required. The value of the id attribute, or name attribute, of the element you want to return.


Return Value

An Element object, representing the element at with specified ID or name.

Returns null if the element does not exist.


Related Pages

HTMLCollection: item() Method

HTMLCollection: length Property


HTMLCollection