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.

Syntax

Return the disabled property:


buttonId.disabled

// 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. 

Examples

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");
}

0 Comments

Leave a reply

Your email address will not be published. required fields are marked *