Javascript DOM HTML Textarea required Property get

Introduction

Find out if a text area must be filled out before submitting a form:

var x = document.getElementById("myTextarea").required;

Click the button to find out if the text area must be filled out before submitting the form.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
Text area:<br>
<textarea id="myTextarea" name="adr" required>
</textarea><br>
<input type="submit">
</form>// w  ww. j a  va  2  s  . c  o m
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

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

</body>
</html>

The required property sets or gets whether a text area must be filled out before submitting a form.

This property mirrors the HTML required attribute.

Value Description
true The text area is a required part of form submission
false Default. The text area is not a required part of form submission

The required property returns true if the text area is a required part of form submission, otherwise it returns false




PreviousNext

Related