Javascript DOM HTML Input Search required Property get

Introduction

Find out if a search field must be filled out before submitting a form:

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

Click the button to find out if the search field must be filled out before submitting the form.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Search: <input type="search" id="mySearch" name="filter" required>
  <input type="submit">
</form>//from  w  w  w  . j  a va 2s  .  c  o m
<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

The readOnly property sets or gets whether a search field must be filled out before submitting a form.

This property mirrors the HTML required attribute.

The readOnly property accepts and returns a boolean type value.

Value Description
true The search field is a required part of form submission
falseDefault. The search field is not a required part of form submission

The readOnly property returns true if the search field is a required part of form submission, otherwise it returns false.




PreviousNext

Related