Open In App

HTML | DOM Textarea readOnly Property

Last Updated : 21 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

 The DOM Textarea readOnly Property is used to set or return that the text in the textarea should be readOnly or not. If it is readOnly then the content cannot be modified but it can be copied.
Syntax: 

  • It is used to return the readOnly property.
textareaObject.readOnly
  • It is used to set the readOnly property.
textareaObject.readOnly = true|false

Property Values: 

  • true: It defines that the text area is readOnly.
  • false: It is the default value. It defines that the text area is not readOnly and can be changed.

Return Value: It returns the value of readOnly property in the form of boolean value i.e. either true or false.
Example-1: This example illustrates how to set the Textarea readonly property.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM textarea readonly Property</title>
</head>
 
<body style="text-align:center">
    <h1 style="color: green;">
      GeeksforGeeks
  </h1>
    <h2>DOM textarea readonly Property</h2>
 
 
    <!-- Assigning id to textarea. -->
    <textarea id="GFG_ID" rows="3" cols="10" name="Geeks" >
This text is wrapped in the text area field.
    </textarea>
    <br>
   
    <button onclick="myGeeks()">Submit</button>
    <p id = "GFG" > </p>
 
 
    <script>
        function myGeeks() {  
          // Set the readOnly property.
          var x =
           document.getElementById(
           "GFG_ID").readOnly = true;
 
          document.getElementById("GFG").innerHTML =
           "The value is set to " + x;
        }
    </script>
</body>
 
</html>


Output: 
Before clicking on button: 
 

After clicking on button : 
 

Example-2 : This example illustrates how to return the Textarea readOnly property
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM textarea readOnly Property</title>
</head>
 
<body style="text-align:center">
   
    <h1 style="color: green;">GeeksforGeeks</h1>
    <h2>DOM textarea readonly Property</h2>
 
    <!-- Assigning id to textarea. -->
    <textarea id="GFG_ID" rows="3" cols="10" name="Geeks" readonly>
This text in the text area field is readOnly.
    </textarea>
    <br>
   
    <button onclick="myGeeks()">Submit</button>
   
    <p id="sudo"></p>
 
 
   
    <!-- Return the value of the readOnly property -->
    <script>
        function myGeeks() {
 
            var x =
            document.getElementById(
            "GFG_ID").readOnly;
 
            document.getElementById("sudo").innerHTML = x;
        }
    </script>
</body>
 
</html>


Output:
Before clicking on button: 
 

After clicking on button: 
 

Supported Browsers: The browser supported by Textarea readOnly Property are listed below: 
 

  • Google Chrome 1
  • Edge 12
  • Internet Explorer 6
  • Firefox 1
  • Opera 12.1
  • Safari 4

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads