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

removeNamedItem method (attributes)

Browser support:
Removes the attribute with the specified name from the current attributes collection and returns the removed attribute node.
If a default value is defined for the removed attribute, then it will be set after the removal.
In Internet Explorer, the removeNamedItem method has a bug; in case of HTML attributes, it removes the specified attribute node but it keeps the removed value for the new attribute. If no attribute exists with the specified namespace and name, the removeNamedItem method raises an exception.

Syntax:

object.removeNamedItem (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 removeAttribute method)!

Return value:

Returns the removed attribute node or null if the attribute cannot be removed.

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 removeNamedItem method and demonstrates the related bug in Internet Explorer:
<head>
    <script type="text/javascript">
        function MoveValueAttr () {
            var input1 = document.getElementById ("input1");
            var input2 = document.getElementById ("input2");

            if (input1.attributes["value"] !== undefined) {
                var valueAttr = input1.attributes.removeNamedItem ("value");
                if (valueAttr) {
                    input2.setAttributeNode (valueAttr);
                }
                else {
                    alert ("Cannot remove the value attribute!");
                }
            }
            else {
                alert ("The first input field has no value attribute specified!");
            }
        }
    </script>
</head>
<body>
    <input id="input1" value="First input field." />
    <input id="input2" value="" />
    <br /><br />
    <button onclick="MoveValueAttr ();">Move the value attribute of the first input field to the second one!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content