files/en-us/web/api/element/toggleattribute/index.md
{{APIRef("DOM")}}
The toggleAttribute() method of the
{{domxref("Element")}} interface toggles a Boolean attribute (removing it if it is
present and adding it if it is not present) on the given element.
toggleAttribute(name)
toggleAttribute(name, force)
name
toggleAttribute() is called on an HTML element in an HTML document.force {{optional_inline}}
toggleAttribute method "toggles" the attribute named name — removing it if it is present, or else adding it if it is not presenttoggleAttribute method adds an attribute named nametoggleAttribute method removes the attribute named nametrue if attribute name is eventually
present, and false otherwise.
InvalidCharacterError {{domxref("DOMException")}}
name contains one or more characters which
are not valid in attribute names.In the following example, toggleAttribute() is used to toggle the
disabled attribute of an {{HTMLElement("input")}}.
<input value="text" /> <button>toggleAttribute("disabled")</button>
const button = document.querySelector("button");
const input = document.querySelector("input");
button.addEventListener("click", () => {
input.toggleAttribute("disabled");
});
{{ EmbedLiveSample('Examples', '300', '50') }}
{{Specifications}}
{{Compat}}