XML DOM namedItem() Method

Definition and Usage

The namedItem() method retrieves a node or element with a specified name from the collection.

The behavior of this method varies depending on the type of document:

  • If the document is an HTML document, this method will first query nodes with an id attribute matching the given name, and if no matching id attribute exists, it will query nodes with a name attribute matching the given name.

    When querying an HTML document, this method is not case-sensitive.

  • If the document is an XHTML document, this method only queries nodes with matching given name id attributes.

    When querying an HTML document, this method is case-sensitive.

Syntax:

htmlcollectionObject.namedItem(name)
Parameter Description
name The name of the node or element to retrieve.

Return Value

Returns an element or node with the specified id or name attribute. If there is no such node in the HTMLCollection, it returns null.

Description

In JavaScript, treat HTMLCollection as an associative array and use array syntax to place the name in [] to find elements more easily.

Example

var c = document.forms;		//This is an HTMLCollection Object
var address = c.namedItem("address");	//Find <form name="address">
var payment = c["payment"];		//Simpler syntax: Find <form name="payment">
var login = c.login;		//This is also valid: Find <form name="login">