getNamedItem method

Gets an attribute specified with the name property, by using the attributes collection.

Document Object Model (DOM) Level 3 Core Specification, Section 1.4



var retval = attributes.getNamedItem(bstrName);


bstrName [in]

Type: BSTR

A String that specifies the name of the attribute to get.

Return value

Type: IHTMLDOMAttribute

Returns an attribute with the specified name.

Standards information


If the attribute applies to an element but is not specified, this method returns the attribute with the specified name set to an empty string.

If the attribute does not apply to the element and is not specified, then an error is returned.

If the attribute does not apply to the element and is specified, then the attribute with the specified name is returned.

Windows Internet Explorer 8 and later. In IE8 Standards mode, the getNamedItem method does not create Attr objects that do not exist in the collection; it returns null when an attribute is not found. For more information on IE8 mode, see Defining Document Compatibility.

getNamedItem was introduced in Microsoft Internet Explorer 6.


The following example shows how to use the getNamedItem method to retrieve the value of an attribute.

Code example:

<!DOCTYPE html>

  <title>getNamedItem Example</title>

  <h1>getNamedItem Method Example</h1>
  <p id="oElem">An element with the <strong>id</strong> attribute set to <strong>oElem</strong>.</p>
    var oAttrColl = oElem.attributes;
    var oAttr = oAttrColl.getNamedItem("id");
    alert("ID Attribute value: " + oAttr.value);


See also