HTML | DOM Textarea required Property

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 the textarea field must be filled out before submittted 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

Before Clicking On Tryit Button:

After Clicking On Tryit Button:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output :

Before Clicking On Button:

After Clicking On Button:

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

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.