Javascript Reference - HTML DOM Input Text readOnly Property








The readOnly property sets or gets whether a text field is read-only.

Browser Support

The readOnly property is supported in all major browsers.

readOnly Yes Yes Yes Yes Yes

Syntax

Return the readOnly property.

var v = textObject.readOnly 

Set the readOnly property.

textObject.readOnly=true|false 

Property Values

Value Description
true|false Specifies whether a text field should be read-only
  • true - The text field is read-only
  • false - Default. The text field is changeable, not read-only




Return Value

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

Example

The following code shows how to get if a text field is read-only or not.


<!DOCTYPE html>
<html>
<body>
Name: <input type="text" id="myText" readonly>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from w  ww . ja  v a  2 s.co m-->
    var x = document.getElementById("myText").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 text field to read-only.


<!DOCTYPE html>
<html>
<body>
Name: <input type="text" id="myText">
<button onclick="myFunction()">test</button>
<!--from ww  w  .  j a v a  2s  .  c om-->
<script>
function myFunction() {
    document.getElementById("myText").readOnly = true;
}
</script>

</body>
</html>

The code above is rendered as follows: