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>
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
Mozilla Developer Network : [getNamedItem Article]
Microsoft Developer Network: [getNamedItem Method Article]