Easy Tutorial
❮ Prop Style Display Prop Style Right ❯

HTMLCollection namedItem() Method

DOM HTMLCollection


Definition and Usage

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

You can also use the following shorthand to retrieve it:

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

Syntax

HTMLCollection.namedItem(name)

or:

HTMLCollection[name]

Parameter Details:

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

Return Value

Returns an 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

Retrieve the element with the ID "myElement" from the p elements:

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

Related Articles

HTMLCollection: item() Method

HTMLCollection: length Property


DOM HTMLCollection

❮ Prop Style Display Prop Style Right ❯