Skip to content
Related Articles

Related Articles

Improve Article

HTML | DOM Textarea required Property

  • Last Updated : 11 Feb, 2019

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.






<!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 Tryit Button:

After Clicking On Tryit Button:

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




<!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
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :