Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTMLCollection namedItem() method

HTMLCollection namedItem() method

DOM HTMLCollection Object Reference DOM HTMLCollection


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 Object Reference DOM HTMLCollection