JavaScript removeAttribute() to Remove an HTML Attribute
Call element.removeAttribute('attribute-name') to remove an HTML attribute in JavaScript. If it is already absent, the element stays as it is. The method returns undefined. Use hasAttribute() to check the result.
Remove an attribute and check the result
The example sets data-mode on a new button, saves its value, and removes it. getAttribute() returns '' for a present but empty attribute and null for an absent one. Check previous !== null before restoring, so an originally empty value can be restored too. DOM Standard · Element.removeAttribute()
const button = document.createElement('button');
button.setAttribute('data-mode', '');
const previous = button.getAttribute('data-mode');
console.log(button.hasAttribute('data-mode')); // true
console.log(previous === ''); // true
console.log(button.removeAttribute('data-mode')); // undefined
console.log(button.hasAttribute('data-mode')); // false
console.log(button.getAttribute('data-mode')); // null
if (previous !== null) {
button.setAttribute('data-mode', previous);
}
console.log(button.hasAttribute('data-mode')); // true
Distinguish empty values from absent boolean attributes
An HTML boolean attribute such as disabled is true when present. Even setAttribute('disabled', 'false') makes the button’s own disabled state true. Remove the attribute or set the button’s disabled property to false to clear that state. HTML Standard · Boolean attributes
const button = document.createElement('button');
button.setAttribute('disabled', 'false');
console.log(button.disabled); // true
button.removeAttribute('disabled');
console.log(button.disabled); // false
button.setAttribute('disabled', '');
console.log(button.disabled); // true
button.disabled = false;
console.log(button.hasAttribute('disabled')); // false
A button can still be disabled inside a disabled fieldset after you remove its own attribute. Check hasAttribute('disabled') for the attribute and matches(':disabled') for the effective disabled state. HTML Standard · The disabled attribute
Remove and restore disabled in a complete example
Save the code in UTF-8 as remove-attribute.html and open it in a browser. The target button starts disabled. Use the remove button to delete the attribute and enable the target. The restore button adds the attribute back with an empty value, which disables the target again.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Try removing disabled</title>
</head>
<body>
<button id="target" type="button" disabled>Target button</button>
<button id="enable" type="button">Remove disabled</button>
<button id="disable" type="button">Restore disabled</button>
<p id="status" aria-live="polite"></p>
<script>
const target = document.querySelector('#target');
const status = document.querySelector('#status');
function show() {
status.textContent = target.hasAttribute('disabled')
? "Target button: disabled" : "Target button: enabled";
}
document.querySelector('#enable').addEventListener('click', () => {
target.removeAttribute('disabled');
show();
});
document.querySelector('#disable').addEventListener('click', () => {
target.setAttribute('disabled', '');
show();
});
show();
</script>
</body>
</html>
Removing the attribute twice leaves the same state. In your developer tools, watch disabled disappear and reappear in the Elements panel. In a form, enable a button only when the intended conditions are met, such as valid inputs.




