Javascript Reference - HTML DOM Input Password readOnly Property








The readOnly property sets or gets if a password field is read-only.

Browser Support

readOnly Yes Yes Yes Yes Yes

Syntax

Return the readOnly property.

var v = passwordObject.readOnly 

Set the readOnly property.

passwordObject.readOnly=true|false 

Property Values

Value Description
true|false  Set if a password field should be read-only.
  • true - The password field is read-only
  • false - Default. The password field is changeable, not read-only




Return Value

A Boolean type value, true if the password field is read-only, otherwise false.

Example

The following code shows how to check if a password field is read-only.


<!DOCTYPE html>
<html>
<body>
<!-- ww  w.  ja v a  2 s.co m-->
Password:<input type="password" id="myPsw" readonly>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("myPsw").readOnly;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to set a password field to read-only.


<!DOCTYPE html>
<html>
<body>
<!--   w  w  w.j  a v a  2  s .  c  om-->
Password: <input type="password" id="myPsw">
<button onclick="myFunction()">Set read-only</button>
<script>
function myFunction() {
    document.getElementById("myPsw").readOnly = true;
}
</script>

</body>
</html>

The code above is rendered as follows: