Javascript Reference - HTML DOM Input Text disabled Property








The disabled property disables or enables a text field.

Browser Support

disabled Yes Yes Yes Yes Yes

Syntax

Return the disabled property.

var v = textObject.disabled 

Set the disabled property.

textObject.disabled=true|false 

Property Values

Value Description
true|false Set whether a text field should be disabled.
  • true - The text field is disabled
  • false - Default. The text field is not disabled




Return Value

A Boolean type value, true if the text field is disabled, otherwise false

Example

The following code shows how to check if a text field is disabled.


<!DOCTYPE html>
<html>
<body>
<!--from   ww  w. j a  v a  2  s. c  om-->
Name: <input type="text" id="myText">
<button onclick="myFunction()">test</button>
<p id="demo"></p>

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

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to disable and enable a text field.


<!DOCTYPE html>
<html>
<body>
<!--from   w  ww  .ja va  2s . c  o  m-->
First Name: <input type="text" id="myText" value="Mickey"><br>

<button onclick="disableTxt()">Disable Text field</button>
<button onclick="enableTxt()">Enable Text field</button>

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

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

</body>
</html>

The code above is rendered as follows: