This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

getNamedItem

Summary

Gets an attribute with a given name from an element.

Method of dom/NamedNodeMapdom/NamedNodeMap

Syntax

var attribute = attributes.getNamedItem(/* see parameter list */);

Parameters

name

Data-type
String

The name of the attribute to get.

Return Value

Returns an object of type DOM NodeDOM Node

The attribute node with the given name.

Examples

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

<!doctype html>
<html>
<head>
<title>getNamedItem Example</title>
<script>
function Init()
{
    var oAttrColl = document.getElementById("oElem").attributes;
    var oAttr = oAttrColl.getNamedItem("align");
    console.log("ALIGN attribute value: " + oAttr.value);
}
  </script>
 </head>
 <body onload="Init()">
  <p id="oElem" align="center">An element.</P>
 </body>
</html>

View live example

Notes

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.

Related specifications

DOM Level 3 Core
Recommendation

Attributions