The readOnly
property sets or gets whether a text field is read-only.
The readOnly property is supported in all major browsers.
readOnly |
Yes | Yes | Yes | Yes | Yes |
Return the readOnly property.
var v = textObject.readOnly
Set the readOnly property.
textObject.readOnly=true|false
Value | Description |
---|---|
true|false | Specifies whether a text field should be read-only
|
A Boolean type value, returns true if the text field is read-only, otherwise false.
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:
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: