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.
<!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 |
---|---|
true | The 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.