HTMLCollection namedItem() Method

Definition and Usage

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

You can also use a shorthand method and it will produce the same result:

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

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

Syntax

HTMLCollection.namedItem(name)

Or:

HTMLCollection[name]

Parameter value

Parameter Description
name Required. The value of the id attribute or name attribute of the element to be returned.

Return value

Element objectRepresents an element with a specified ID or name.

Returns null if the element does not exist.

Browser Support

Method Chrome IE Firefox Safari Opera
namedItem() Support Support Support Support Support

Related Pages

HTMLCollection:item() Method

HTMLCollection:length Property