Javascript Reference - HTML DOM Button disabled Property








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

Browser Support

disabled Yes Yes Yes Yes Yes

Syntax

Return the disabled property:

var v = buttonObject.disabled 

Set the disabled property:

buttonObject.disabled=true|false

Property Values

Value Description
true|false Specifies whether a button should be disabled or not




Return Value

A Boolean, returns true if the button is disabled, otherwise it returns false.

Example

The following code shows how to disable a button.


<!DOCTYPE html>
<html>
<body>
<!-- w  w  w.ja  va  2s  . co  m-->
<button id="myBtn">My Button</button>
<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("myBtn").disabled = true;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to find out if a button is disabled.


<!DOCTYPE html>
<html>
<body>
<!--from  w  w w  .  j  a v a  2s .co  m-->
<button id="myBtn" disabled>My Button</button>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myBtn").disabled;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to disable and enable a button.


<!DOCTYPE html>
<html>
<body>
<!--from   w w  w .  jav  a  2 s .c  o m-->
<button id="myBtn">My Button</button>
<br><br>

<button onclick="disableBtn()">Disable</button>
<button onclick="undisableBtn()">enable"</button>

<script>
function disableBtn() {
    document.getElementById("myBtn").disabled = true;
}

function undisableBtn() {
    document.getElementById("myBtn").disabled = false;
}
</script>

</body>
</html>

The code above is rendered as follows: