Javascript Reference - HTML DOM Textarea readOnly Property








The readOnly property sets or gets whether the textarea is read-only.

Browser Support

readOnly Yes Yes Yes Yes Yes

Syntax

Return the readOnly property.

var v = textareaObject.readOnly 

Set the readOnly property.

textareaObject.readOnly=true|false 

Property Values

Value Description
true|false Set whether a textarea is read-only.
  • true - The textarea is read-only
  • false - Default. The textarea is changeable




Return Value

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

Example

The following code shows how to set a textarea to be read-only.


<!DOCTYPE html>
<html>
<body>
<textarea id="myTextarea">
this is a test<!--from  w ww .  j a  v  a2  s. com-->
</textarea>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myTextarea").readOnly = "true";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to check if a textarea is read-only or not.


<!DOCTYPE html>
<html>
<body>
<textarea id="myTextarea" readonly>
this is a test<!--from   w ww.jav  a  2 s  .co m-->
</textarea>
<button type="button" onclick="myFunction()">test</button>
<p id="demo"></p>

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

</body>
</html>

The code above is rendered as follows: