You are here: Reference > JavaScript > client-side > HTML DOM > methods > getNamedItem (attributes)

getNamedItem method (attributes)

Browser support:
Returns the attribute node with the specified name from the current attributes collection.
To get the value of an attribute, you do not need to work with attribute nodes; the use of the getAttribute method is simpler.
The attribute nodes of the attributes collection are also accessible like the elements of an associative array. See Example 2 for details.

Syntax:

object.getNamedItem (attributeName);
You can find the related objects in the Supported by objects section below.

Parameters:

attributeName
Required. String that specifies the name of the attribute. The name is case-sensitive in XML documents and case-insensitive in HTML documents. If you want to get an HTML attribute, use the name of the HTML attribute instead of the corresponding JavaScript property name in Internet Explorer (unlike in the case of the getAttribute method)!

Return value:

If no attribute is specified with the given name, it returns null, else it returns the matching attribute node.
In Internet Explorer before version 8, if an attribute has a default value and the attribute is not specified, then the getNamedItem method retrieves an attribute node representing the default value. If you want to detect whether an element has an attribute with the given name, use the specified property of the returned attribute node.

Methods for attributes without namespaces:

Name Browser Description
createAttribute
Creates a new attribute node with the specified name.
getAttribute
Returns the value of the attribute with the specified name from the current element.
getAttributeNode
Returns the attribute node with the specified name from the current element.
getNamedItem
Returns the attribute node with the specified name from the current attributes collection.
hasAttribute
8
Returns whether the current element has an attribute with the specified name or not.
removeAttribute
Removes the attribute with the specified name from the current element.
removeAttributeNode
Removes the specified attribute node from the current element.
removeNamedItem
Removes the attribute with the specified name from the current attributes collection and returns the removed attribute node.
setAttribute
Adds an attribute with the specified name and value to the current element.
setAttributeNode
Adds the specified attribute node to the current element.
setNamedItem
Adds the specified attribute node to the current attributes collection.

Example HTML code 1:

This example illustrates the use of the getNamedItem method:
<head>
    <script type="text/javascript">
        function GetOnClickAttrValue (button) {
            var onclickAttr = button.attributes.getNamedItem ("onclick");
            alert ("The value of the onclick attribute: " + onclickAttr.value);
        }
    </script>
</head>
<body>
    <button onclick="GetOnClickAttrValue (this)">Get the value of my onclick attribute!</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example is equivalent to the previous one, but it uses the attributes collection as an associative array:
<head>
    <script type="text/javascript">
        function GetOnClickAttrValue (button) {
            var onclickAttr = button.attributes["onclick"];
            alert ("The value of the onclick attribute: " + onclickAttr.value);
        }
    </script>
</head>
<body>
    <button onclick="GetOnClickAttrValue (this)">Get the value of my onclick attribute!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content