The removeNamedItem() method of a NamedNodeMapremoves an attribute by name and returns the removed Attr node. Learn the MDN element.attributes example, what happens when the attribute is missing, and how it compares with removeAttribute()—with five examples and try-it labs.
01
Kind
Instance method
02
Returns
Removed Attr
03
Arg
attrName
04
Missing
Throws error
05
Use case
Drop attrs
06
Status
Baseline widely
Fundamentals
Introduction
getNamedItem() reads an attribute from element.attributes. removeNamedItem() does the opposite—it deletes the attribute from the element and hands you back the Attr node that was removed.
MDN’s example removes a test attribute from a <pre> element, then checks with getNamedItem("test") to confirm it is gone.
💡
Beginner tip
For most everyday HTML tasks, call element.removeAttribute("class") instead. Use attributes.removeNamedItem("class") when you need the removed Attr node or are already walking the NamedNodeMap.
Concept
Understanding the removeNamedItem() Method
An instance method on NamedNodeMap that removes an attribute node by its name string.
attrName — the name of the attribute to remove (e.g. "test", "class").
Returns — the removed Attr node.
Exception — thrown when no attribute with that name exists (MDN).
Side effect — the attribute disappears from the element in the DOM.
Common source — element.attributes.removeNamedItem(name).
Baseline Widely available on MDN (since July 2015).
Foundation
📝 Syntax
Call the method on a NamedNodeMap (usually from element.attributes):
JavaScript
namedNodeMap.removeNamedItem(attrName)
Parameters
attrName — string name of the attribute to remove.
Return value
The removed Attr node.
Exceptions
Thrown if there is no attribute with the given name.
Typical pattern (MDN idea)
JavaScript
const pre = document.querySelector("pre");
const attrMap = pre.attributes;
let result = `The 'test' attribute initially contains '${attrMap["test"].value}'.\n`;
result += "We remove it.\n\n";
attrMap.removeNamedItem("test");
result += attrMap.getNamedItem("test")
? "And 'test' still exists."
: "And 'test' is no more to be found.";
pre.textContent = result;
MDN pattern: remove an attribute and verify it is gone.
Example 1 — MDN Remove test Attribute
Read the value, remove it, then confirm with getNamedItem.
JavaScript
const pre = document.querySelector("pre");
const attrMap = pre.attributes;
let result = `The 'test' attribute initially contains '${attrMap["test"].value}'.\n`;
result += "We remove it.\n\n";
attrMap.removeNamedItem("test");
result += attrMap.getNamedItem("test")
? "And 'test' still exists."
: "And 'test' is no more to be found.";
pre.textContent = result;
NamedNodeMap.removeNamedItem() is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
NamedNodeMap.removeNamedItem()
Removes an attribute by name from the map and returns the removed Attr—or throws if not found.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported on NamedNodeMap (legacy DOM)
Legacy OK
NamedNodeMap.removeNamedItem()Excellent
Bottom line: Call map.removeNamedItem(attrName) on element.attributes when you need the removed Attr node back.
Wrap Up
Conclusion
NamedNodeMap.removeNamedItem() removes an attribute by name and returns the removed Attr. Use it on element.attributes, handle missing names with try/catch, and prefer removeAttribute() when you only need to delete a value.
Store the returned Attr if you need name/value after removal
Remember map.length changes after removal
❌ Don’t
Call removeNamedItem on a name that may not exist without handling errors
Assume it returns null like getNamedItem
Remove attributes while forward-looping the same map without care
Confuse it with removeAttributeNode(attr) (takes Attr, not name)
Use it when removeAttribute is simpler and sufficient
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about removeNamedItem()
Remove attributes by name from NamedNodeMap.
5
Core concepts
🔗01
Returns
Removed Attr
API
⚙️02
Arg
attrName
Params
⚠️03
Missing
throws
MDN
📄04
length
decreases
Side effect
🎯05
Baseline
since Jul 2015
Status
❓ Frequently Asked Questions
It removes the Attr node with the given name from the map and returns that removed Attr. On elements, element.attributes.removeNamedItem('class') removes the class attribute from the element.
No. MDN marks NamedNodeMap.removeNamedItem() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
The removed Attr node. You can read its .name and .value after removal—the node still exists in memory even though it is no longer on the element.
MDN documents an exception: the method throws if there is no attribute with the given name (typically a NotFoundError DOMException).
removeNamedItem() works on the NamedNodeMap (element.attributes) and returns an Attr. element.removeAttribute(name) works on the element and returns undefined—it only removes the string value.
When you need the removed Attr node back, or when you are already working with element.attributes. For everyday HTML, removeAttribute() is usually simpler.
Did you know?
MDN’s removeNamedItem() demo prints “And ’test’ is no more to be found.” after removal—because getNamedItem("test") returns null once the attribute is gone.