Definition and Usage
The removeAttribute() function removes the attribute specified as a parameter from a DOM element.
If the specified attribute does not exist on the element, it does nothing without throwing an error.
Syntax
element.removeAttribute(attributeName)
Parameters
attributeName |
Required. The name of the attribute to remove from the element (a string). |
|---|
Because the parameter is required, omitting it throws an error.
However, if the attribute specified as the parameter does not exist on the element, the function does nothing without throwing an error.
Return Value
None (undefined).
Basic Example
<style>
.bg {
background-color: yellowgreen;
}
</style>
<p class="bg">Try removing the background color.</p>
<button type="button">Remove Background Color</button>
const elem = document.querySelector("p");
const button = document.querySelector("button");
button.addEventListener("click", () => { // Button click event
/* When the button is clicked,
the class attribute is removed from the p element. */
elem.removeAttribute("class");
});
Code Explanation
The querySelector() function returns the first element that matches the specified CSS selector within the HTML document or a specific element.
"Remove Background Color" button!
Try removing the background color.
Removing Boolean Attributes
Some HTML attributes are written using only the attribute name without specifying a value. These are called Boolean attributes.
The disabled, hidden, and required attributes are some of the most commonly used Boolean attributes in HTML. Their functionality is determined solely by whether they are present on an element.
Removing these Boolean attributes with the removeAttribute() function also disables the functionality they provide.
Example
The following example contains a single-line text input field (<input type="text">) with the disabled attribute. Because the disabled attribute is present, the text input field is disabled, so you cannot enter or edit text. However, removing this attribute with the removeAttribute() function enables the text input field again, allowing it to be used.
<input type="text" value="Hello World!" disabled>
<button type="button">Enable<</button>
const input = document.querySelector("input");
const button = document.querySelector("button");
button.addEventListener("click", () => {
input.removeAttribute("disabled");
});
Then, click the
'Enable' button and check whether the text input field becomes enabled again.
Browser Compatibility
| Method |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
removeAttribute()
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
removeAttribute()
|
DOM Standard #ref-for-dom-element-removeattribute① |
References
See Also
- JavaScript getAttribute() Function – Getting Attribute Values from Elements
- JavaScript setAttribute() Function – Setting or Adding Attributes to Elements
- JavaScript hasAttribute() Function – Checking If an Element Has an Attribute
- JavaScript toggleAttribute() Function – Toggling a Boolean Attribute on an Element
- JavaScript classList.remove() – Remove Class Values from an Element