HTML DOM Textarea required Property

HTMLDOMWeb DevelopmentFront End Technology

The HTML DOM Textarea required property returns and modify the value of the required attribute of a text area in an HTML document.

Syntax

Following is the syntax −

1. Returning required

object.required

2. Modifying required

object.required = true | false

Let us see an example of HTML DOM Textarea required Property:

Example

Live Demo

<!DOCTYPE html>
<html>
<head>
<title>DOM Textarea required Property</title>
<style>
   body {
      text-align: center;
   }
   .btn {
      display: block;
      margin: 1rem auto;
      background-color: #db133a;
      color: #fff;
      border: 1px solid #db133a;
      padding: 0.5rem;
      border-radius: 50px;
      width: 20%;
   }
   .show-message {
      font-weight: bold;
      font-size: 1.4rem;
      color: #ffc107;
   }
</style>
</head>
<body>
<h1>DOM Textarea required Property Demo</h1>
<form id="form" method="post" action="">
<fieldset>
<legend>Form </legend>
<textarea rows="5" cols="20" required placeholder="Enter a message"></textarea>
<input type="submit" class="btn" value="Submit Form" onclick="checkRequired()">
</fieldset>
</form>
<div class="show-message"></div>
<script>
   function checkRequired() {
      var textarea = document.querySelector("textarea");
      if (textarea.value === "") {
         document.querySelector(".show-message").innerHTML = "Please enter a message!";
      }
   }
</script>
</body>
</html>

Output

Click on “Submit Form” button without entering any message in textarea element.

raja
Published on 20-Sep-2019 15:06:32
Advertisements