Element.toggleAttribute() toggles a Boolean attribute: removes it if present, or adds it if missing (MDN). Ideal for flags like disabled, hidden, or readonly. Returns true if the attribute is present after the call, else false.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call el.toggleAttribute. Start with this line.
JavaScript
element.toggleAttribute(name)
Call
What it does
element.toggleAttribute(name)
Primary signature.
el.toggleAttribute(...)
Call with the arguments your case needs.
Built into the browser. Call toggleAttribute(...) on an Element — no import required.
Library declarations
JavaScript
element.toggleAttribute(name)
Parameters
Parameter
Description
name
Required. Attribute to toggle (lowercased on HTML elements)
Update UI from that value. No extra hasAttribute needed.
Clearer than guessing the previous state yourself.
Compatibility
Browser Support
Element.toggleAttribute() is Baseline Widely available on MDN (across browsers since October 2018). Safe to use for boolean attribute toggles in modern apps.
✓ Baseline Widely available
Element.toggleAttribute()
Toggle boolean attributes — widely supported since 2018.
WideBaseline
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
toggleAttribute()Widely available
Bottom line: Use toggleAttribute(name) to flip flags like disabled. Pass force true/false when you need a definite end state. Returns whether the attribute is present afterward.
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 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 for set-or-remove with the same API.
On an HTML element in an HTML document, the name is automatically converted to lower-case (MDN).