Javascript DOM HTML Input Checkbox required Property get

Introduction

Find out if a checkbox must be checked before submitting a form:

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

Click button to find out if the checkbox must be checked before submitting the form.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Checkbox: <input type="checkbox" id="myCheck" name="test" required>
  <input type="submit">
</form>/*from   w  ww.j  a  v a 2  s .  co  m*/
<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

The required property sets or gets whether a checkbox must be checked before submitting a form.

This property mirrors the HTML required attribute.

The required property accepts and returns a boolean value.

Value Description
trueThe checkbox must be checked before submitting a form
false Default. The checkbox is not a required part of form submission

It returns true if the checkbox must be checked before submitting a form, otherwise it returns false.




PreviousNext

Related