Element.removeAttribute() is an instance method that deletes an HTML attribute by name. Learn the MDN disabled example, removing class and data-* attributes, why MDN prefers this over setAttribute(name, null), and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Arg
attrName
03
Returns
undefined
04
Missing
no error
05
vs null
prefer remove
06
Status
Baseline widely
Fundamentals
Introduction
HTML attributes like disabled, class, and data-action live on elements as name–value pairs. To delete an attribute in JavaScript, call element.removeAttribute(attrName). MDN: it removes the attribute with the specified name from the element.
If the attribute is not present, removeAttribute() does nothing and does not throw—a safe no-op (MDN).
💡
Beginner tip
MDN recommends removeAttribute() instead of setting an attribute to null with setAttribute(). Many attributes do not behave correctly when set to null.
Calling el.removeAttribute(attrName) removes that attribute from el’s attribute list if it exists.
It is an instance method on Element.
Takes one parameter: attrName (string) — the attribute to remove (MDN).
Returns undefined.
No error if the attribute does not exist (MDN).
Preferred over setAttribute(name, null) per MDN usage notes.
Pairs with setAttribute(), getAttribute(), and hasAttribute().
Foundation
📝 Syntax
General form of Element.removeAttribute (MDN):
JavaScript
removeAttribute(attrName)
Parameters
Parameter
Type
Description
attrName
string
The name of the attribute to remove from the element (MDN).
Return value
Type
Description
undefined
No return value (MDN).
Usage notes
MDN: use removeAttribute() instead of setting the attribute value to null directly or via setAttribute(). Many attributes will not behave as expected if set to null.
Common patterns
JavaScript
// MDN example — enable a disabled div
document.getElementById("div1").removeAttribute("disabled");
// Remove class or data-* attributes
el.removeAttribute("class");
el.removeAttribute("data-action");
// Safe — no error if missing
if (el.hasAttribute("hidden")) {
el.removeAttribute("hidden");
}
Cheat Sheet
⚡ Quick Reference
Goal
Code
Remove an attribute
el.removeAttribute("disabled")
Remove class
el.removeAttribute("class")
Remove data-*
el.removeAttribute("data-id")
Check before remove
el.hasAttribute("hidden")
Missing attribute
No error (no-op)
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Element.removeAttribute().
After removal, both hasAttribute and dataset reflect that the custom data is gone.
Example 4 — Missing Attribute Is Safe (MDN)
Calling removeAttribute on a non-existent attribute does not throw.
JavaScript
const el = document.getElementById("box");
el.removeAttribute("hidden"); // no error — attribute was not set
el.removeAttribute("hidden"); // still no error
console.log("done");
Element.removeAttribute() is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.removeAttribute()
Delete any HTML attribute by name — no error when the attribute is missing.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerSupported in legacy IE
Yes
removeAttribute()Excellent
Bottom line: Use removeAttribute() instead of setAttribute(name, null). Pair with hasAttribute() and getAttribute() for attribute workflows.
Wrap Up
Conclusion
Element.removeAttribute() deletes an HTML attribute by name. MDN recommends it over setting values to null, and it safely no-ops when the attribute is missing.
Remove class when you only need to drop one class name
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.removeAttribute()
Delete attributes by name — safely.
5
Core concepts
📝01
Returns
undefined
API
📄02
Arg
attrName
Name
🗑️03
Missing
no error
Safe
✅04
vs null
prefer remove
MDN
⚡05
Pair
hasAttr
Check
❓ Frequently Asked Questions
It removes the attribute with the specified name from the element (MDN).
No. MDN marks Element.removeAttribute() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
A string with the name of the attribute to remove, such as "disabled", "class", or "data-id" (MDN).
Nothing useful — the return value is undefined (MDN).
MDN: removeAttribute() returns without generating an error. It is a safe no-op.
MDN recommends removeAttribute() instead of setting the attribute value to null. Many attributes do not behave as expected when set to null.
Did you know?
MDN: use removeAttribute(attrName) instead of setting an attribute to null. Many boolean and enumerated attributes do not behave correctly when their value is set to null.