Yogesh Chauhan's Blog

How to enable and disable button using JavaScript?

in JavaScript on June 17, 2020

We can use Button's disabled Property to do do.

Button disabled Property

The disabled property sets or returns whether a button is disabled, or not.

A disabled element is the one which is unusable and un-clickable.

That means users won't be able to click on it. You might think, why would we even need one then? Well, in many cases, you want to enable the button when user performs some kinds of action first. 

Disabled elements are usually rendered in gray by default in browsers.

This property reflects the HTML disabled attribute.

It is supported in all major browsers.


Return the disabled property:


// not necessarily only Id though

Set the disabled property:

buttonId.disabled = true or false (default is false)

// not necessarily only Id though

Where true and false specifies whether a button should be disabled or not. true indicates that the button is disabled. false indicates that the button is not disabled. 


Disable the button using JavaScript

document.getElementById("myBtn").disabled = true;

Enable the button using JavaScript

document.getElementById("myBtn").disabled = false;

Find out if a button is disabled or not:

var x = document.getElementById("myBtn").disabled;

if (x == true) {
  console.log("button is disabled");
} else {
  console.log("button is enabled");

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
A Quick Guide to Object-Oriented Programming in PHPPHPFULL OUTER JOIN in PostgresPostgresThe difference between isFinite() method and isFinite() function in JavaScriptJavaScriptUnary Operators in JavaScriptJavaScriptEffects in ReactReactHow to hide and show menu on scroll using JavaScript and CSS?CSS