Definition and Usage
Syntax
element.toggleAttribute(attributeName)
element.toggleAttribute(attributeName, force)
Parameters
attributeName |
Required. The name of the Boolean attribute to be toggled (a string).
This parameter is not case-sensitive. |
|---|---|
force |
Optional. A Boolean value (true or false) that forces the attribute to be added or removed.
|
Return Value
Examples
The following three examples demonstrate how to use the toggleAttribute() function.
Basic Usage
Additional Explanation
The hidden attribute indicates that an element is hidden and should not be visible.
<input type="text">
<button type="button">Hide/Show Input Box</button>
const elem = document.querySelector("input");
const button = document.querySelector("button");
button.addEventListener("click", () => { // Button click event
// Adds the hidden attribute if it does not exist; otherwise removes it
elem.toggleAttribute("hidden");
});
Code Explanation
The querySelector() function returns the first element that matches the specified CSS selector within the HTML document or a specific element.
"Hide/Show Input Box" button!
Using the Return Value
This example demonstrates how to use the return value of the toggleAttribute() function.
It returns true when the specified attribute is added, and false when it is removed.
<input type="text">
<button type="button">Hide/Show Input Box</button>
<p style="color: red;">
The status of the hidden attribute will be displayed here.
</p>
button.addEventListener("click", () => { // Button click event
if (inputElem.toggleAttribute("hidden")) { // If the hidden attribute is added
msgElem.textContent = "The hidden attribute has been added.";
} else { // If the hidden attribute is removed
msgElem.textContent = "The hidden attribute has been removed.";
}
});
Code Explanation
The textContent property gets or sets the text content of an HTML element.
Adding the hidden attribute returns true, while removing it returns false.
"Hide/Show Input Box" button!
The status of the hidden attribute will be displayed here.
Using the Second Parameter (force)
The second parameter (force) of the toggleAttribute() function is optional and can have a value of true or false.
- If set to
true, the specified Boolean attribute is forcibly added regardless of whether it already exists on the element.
(In other words, it forces the Boolean attribute to be present.) - If set to
false, the specified Boolean attribute is forcibly removed regardless of whether it already exists on the element.
(In other words, it forces the Boolean attribute to be absent.)
<input type="text">
<button type="button">Hide/Show Input Box</button>
button.addEventListener("click", () => { // Button click event
if (elem.hasAttribute("hidden")) { // Check whether the hidden attribute exists
elem.toggleAttribute("hidden", false); // Forcefully remove the hidden attribute
} else {
elem.toggleAttribute("hidden", true); // Forcefully add the hidden attribute
}
});
"Hide/Show Input Box" button!
Browser Compatibility
| Method |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
toggleAttribute()
|
69 | 18 | 63 | 12 |
Specifications
| Specification | |
|---|---|
toggleAttribute()
|
DOM Standard #ref-for-dom-element-toggleattribute① |
References
See Also
- JavaScript getAttribute() Function – Getting Attribute Values from Elements
- JavaScript removeAttribute() Function – Removing Specified Attributes from Elements
- JavaScript setAttribute() Function – Setting or Adding Attributes to Elements
- JavaScript hasAttribute() Function – Checking If an Element Has an Attribute
- JavaScript classList.add() – Add Class Values to an Element
- JavaScript classList.remove() – Remove Class Values from an Element
- JavaScript classList.toggle() – Add a Class Value If It Does Not Exist, or Remove It If It Does