HTML DOM Input Password disabled Property


The HTML DOM Input Password disabled property is used for setting or returning whether the password field is disabled or not. It uses boolean values with true representing the element should be disabled and false otherwise. The disabled property is set to false by default. The disabled element is greyed out by default and is unclickable.

Syntax

Following is the syntax for −

Setting the disabled property −

passwordObject.disabled = true|false;

Here, true=password field is disabled and false=the password field is not disabled. It is false by default.

Example

Let us look at an example for the Input password disabled property −

<!DOCTYPE html>
<html>
<body>
<h1>Input Password disabled Property</h1>
Password: <input type="password" id="PASS">
<p>Disable the above password field by clicking on the DISABLE button</p>
<button type="button" onclick="disablePass()">DISABLE</button>
<p id="Sample"></p>
<script>
   function disablePass() {
      document.getElementById("PASS").disabled=true;
      document.getElementById("Sample").innerHTML = "The password field is now disabled" ;
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the DISABLE button −

On clicking the “DISABLE” button −

In the above example −

We have first created an input password field with id “PASS” and it’s disabled property is by default set to FALSE −

Password: <input type="password" id="PASS" value="abcd123">

We have then created a button DISABLE that will execute the disablePass() method when clicked by the user −

<button type="button" onclick="disablePass()">DISABLE</button>

The disablePass() method uses the getElementById() method to get the input field with type password and sets it disabled property to true. This disables the password field and greys it out. We then display a message using the innerHTML property of paragraph with id “Sample” indicating that the password field is now disabled −

function disablePass() {
   document.getElementById("PASS").disabled=true;
   document.getElementById("Sample").innerHTML = "The password field is now disabled" ;
}
raja
Published on 09-Aug-2019 14:19:48
Advertisements