Element.toggleAttribute() is an instance method that toggles a Boolean attribute — adding it when missing, removing it when present. Learn MDN’s disabled input demo, the optional force parameter, the boolean return value, comparison with setAttribute() and removeAttribute(), and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
boolean
03
Args
name, force?
04
Action
Add / remove
05
Best for
Boolean attrs
06
Status
Baseline widely
Fundamentals
Introduction
Many HTML attributes are boolean: their presence means “on” and their absence means “off” — for example disabled, hidden, checked, and readonly.
element.toggleAttribute(name) flips that presence in one call (MDN). You do not need a separate if (hasAttribute) remove else set branch for simple toggles.
💡
Beginner tip
The method returns true when the attribute is present after the call, and false when it is not (MDN). Use that return value to update UI labels or log state without a second hasAttribute() check.
Attribute name to toggle. Lower-cased on HTML elements in HTML documents (MDN).
force
boolean (optional)
If true, add the attribute. If false, remove it. If omitted, toggle (MDN).
Return value
true if attribute name is eventually present; false otherwise (MDN).
Exceptions
InvalidCharacterError — name is empty or contains invalid characters such as ASCII whitespace, NULL, /, =, or > (MDN).
Common patterns
JavaScript
const input = document.querySelector("input");
// Toggle presence (MDN)
input.toggleAttribute("disabled");
// Force on / off
input.toggleAttribute("disabled", true); // always add
input.toggleAttribute("disabled", false); // always remove
// Use return value
const isDisabled = input.toggleAttribute("disabled");
console.log(isDisabled); // true if disabled is now present
Cheat Sheet
⚡ Quick Reference
Goal
Code
Toggle attribute
el.toggleAttribute("disabled")
Force add
el.toggleAttribute("hidden", true)
Force remove
el.toggleAttribute("hidden", false)
Check after toggle
const on = el.toggleAttribute("open")
Existence only
el.hasAttribute("disabled")
Valued attribute
el.setAttribute("id", "x")
Snapshot
🔍 At a Glance
Four facts to remember about Element.toggleAttribute().
Panel visibility flips; button label updates from return value.
How It Works
The HTML hidden attribute hides the element via the browser’s default stylesheet. Toggling it is a clean alternative to class-based show/hide for simple panels.
Example 5 — Guard invalid attribute names
Invalid names throw InvalidCharacterError (MDN).
JavaScript
const el = document.createElement("div");
try {
el.toggleAttribute("ok-name");
console.log("ok-name toggled");
el.toggleAttribute("bad name"); // space is invalid (MDN)
} catch (err) {
console.log(err.name); // InvalidCharacterError
}
Element.toggleAttribute() is Baseline Widely available (MDN: across browsers since October 2018). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.toggleAttribute()
Toggle Boolean attributes — add if missing, remove if present.
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 ExplorerNot supported — use set/removeAttribute polyfill
No
toggleAttribute()Excellent
Bottom line: Use toggleAttribute() for disabled, hidden, and other Boolean attributes. Pass force when you need explicit add or remove. Prefer setAttribute() when you need a value string.
Wrap Up
Conclusion
Element.toggleAttribute() is the clean way to flip Boolean attributes. Use the optional force parameter for explicit add/remove, and the boolean return value to know the final state.
Use for Boolean attributes like disabled and hidden
Use the return value to update UI state
Pass force when syncing from a boolean variable
Prefer lower-case attribute names in HTML
Validate dynamic names before calling (MDN)
❌ Don’t
Use it when you need to set a string value (use setAttribute)
Pass names with spaces or = characters
Assume IE supports it without a polyfill
Toggle valued attributes if you care about preserving values
Ignore the return value when you need the new state
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.toggleAttribute()
One call to flip Boolean attribute presence.
5
Core concepts
📝01
Returns
boolean
Present?
🔁02
Default
toggle
Flip
⚙️03
force
add/remove
Optional
✅04
Best for
boolean attrs
disabled
🌐05
Support
baseline
Oct 2018
❓ Frequently Asked Questions
It toggles a Boolean attribute on the element: removes it if present, or adds it if not present (MDN).
No. MDN marks Element.toggleAttribute() as Baseline Widely available (across browsers since October 2018). It is not Deprecated, Experimental, or Non-standard.
true if the attribute is eventually present after the call, and false otherwise (MDN).
Optional boolean. If true, the attribute is added. If false, it is removed. If omitted, the attribute is toggled (MDN).
toggleAttribute() flips presence in one call and returns whether the attribute is present. setAttribute() always sets a value; removeAttribute() always removes. Use force true/false when you need set or remove semantics with the same API.
On an HTML element in an HTML document, the name is automatically converted to lower-case (MDN).
Did you know?
toggleAttribute() is to Boolean attributes what classList.toggle() is to CSS classes — one call flips state and gives you a boolean back. MDN’s demo uses it to enable and disable an <input> without juggling setAttribute / removeAttribute.