To disable a button using only JavaScript you need to set the disabled property to true. For example: element.disabled = true.
And to enable a button, you need do the opposite by setting the disabled JavaScript property to false.
Here is a more complete example where we select the button and then change its disabled property:
// Disabling a button using JavaScript
document.querySelector('#button').disabled = true;
Code language: JavaScript (javascript)
// Enabling a disabled button
document.querySelector('#button').disabled = false;
Code language: JavaScript (javascript)
These are the steps we have to follow:
- Select the button element you want to disable.
- Set the
disabledproperty tofalse.
The
disabledproperty reflects the HTML attributedisabledand provide a way to change this property dynamically with JavaScript.
Disable Button JS Example
For demo purposes and to keep it as simple as possible, we’ll be disabling the button when clicking on itself:
const button = document.querySelector('#button');
const disableButton = () => {
button.disabled = true;
};
button.addEventListener('click', disableButton);
Code language: JavaScript (javascript)
Here’s the Codepen so you can test it out yourself and play a bit more with it by changing the code:
If you are using jQuery, check out our article on how to disable a button using jQuery. The process is pretty similar.
Disable Button Changing the “disabled” HTML Attribute
Because the disabled property is also available on the DOM, and you can change the state of a button by changing the HTML attribute.
This is how a disabled button would look on HTML markup:
<button id="button" disabled>Save</button>Code language: HTML, XML (xml)
So, to change the HTML attribute disabled you have to use the property setAttribute, then use removeAttribute to re-enable the button.
// Disabling a button using setAttribute
button.setAttribute("disabled", "");
// Re-enabling a disabled button by removing the "disabled" property
button.removeAttribute("disabled");Code language: JavaScript (javascript)
![30+ Best Church Website Templates [WordPress & HTML] church website templates share](https://alvarotrigo.com/blog/wp-content/uploads/2023/08/church-website-templates-share-300x150.png)
![Disable button in jQuery [With examples] disable button jquery share](https://alvarotrigo.com/blog/wp-content/uploads/2023/08/disable-button-jquery-share-300x150.png)
![How To Change CSS With JavaScript [With Examples] change css javascript share](https://alvarotrigo.com/blog/wp-content/uploads/2023/08/change-css-javascript-share-300x150.png)
![19+ Best Music Band Website Templates [Free & Paid] band website templates share](https://alvarotrigo.com/blog/wp-content/uploads/2023/08/band-website-templates-share-300x150.png)
![Change "Select" Option Using JavaScript [With Examples] javascript select option share](https://alvarotrigo.com/blog/wp-content/uploads/2023/08/javascript-select-option-share-300x150.png)
![17+ CSS Round Buttons [Examples & How to create] css round button share](https://alvarotrigo.com/blog/wp-content/uploads/2023/08/css-round-button-share-300x150.png)