Skip to content
Related Articles

Related Articles

HTML | DOM Textarea required Property

View Discussion
Improve Article
Save Article
  • Last Updated : 21 Jul, 2022
View Discussion
Improve Article
Save Article

The DOM Textarea required Property is used to set or return the Whether that the input element must be filled out before submitting the Form. This Property is used to reflect the HTML required attribute.
Syntax: 
 

  • It is used to Return the required property: 
     
textareaObject.required
  • It is used to Set the required property: 
     
textareaObject.required = true|false

Property Values 
 

  • true: It specifies that the textarea field must be filled out before submitted the form.
  • false: It has a Default value. It specifies that the textarea field is not required part of the form.

Return Value: It returns a Boolean value which represents that the textarea field must be filled out or not before submitting the form.
Example-1: In HTML Program that illustrate how to return the required property
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM required textarea Property</title>
    <style>
        h1,
        h2 {
            color: green;
            font-style: italic;
        }
         
        body {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h2>DOM required textarea Property</h2>
   
    <form action="/action_page.php">
        <textarea id="GFG"
                  rows="7"
                  cols="50"
                  name="comment"
                  required>
        </textarea>
       
        <input type="submit">
    </form>
    <br>
    <br>
   
    <button onclick="myGeeks()">
      Try it
    </button>
 
    <p id="sudo"></p>
 
 
 
 
    <script>
        function myGeeks() {
           
            // Return Textarea property
            var x =
            document.getElementById("GFG").required;
           
            document.getElementById("sudo").innerHTML =
              x;
        }
    </script>
</body>
 
</html>

Output: 
Before Clicking On Try it Button: 
 

After Clicking On Try it Button: 
 

Example-2: In HTML Program that illustrate how to set the required property
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM required textarea Property</title>
    <style>
        h1,
        h2 {
            color: green;
            font-style: italic;
        }
         
        body {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h2>DOM required textarea Property</h2>
   
    <form action="/action_page.php">
        <textarea id="GFG"
                  rows="7"
                  cols="50"
                  name="comment">
        </textarea>
       
    <input type="submit">
    </form>
    <br>
    <br>
    <button onclick="myGeeks()">
      Try it
    </button>
 
    <p id="sudo"></p>
 
 
 
 
    <script>
        function myGeeks() {
           
           // Set Textarea property.
           var x =
           document.getElementById("GFG").required =
            true;
           
            document.getElementById("sudo").innerHTML =
             "Now the value of required attribute is set to True";
        }
    </script>
</body>
 
</html>

Output :
Before Clicking On Button: 
 

After Clicking On Button: 
 

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

  • Google Chrome 4 and above
  • Edge 12 and above
  • Internet Explorer 10 and above
  • Firefox 4 and above
  • Opera 12.1 and above
  • Safari 5 and above

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!