Javascript DOM HTML Input Checkbox required Property set

Introduction

Set a checkbox to be a required part of form submission:

document.getElementById("myCheck").required = true;

Click button to set the required property for the checkbox.

View in separate window

<!DOCTYPE html>
<html>
<body>

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

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

<script>
function myFunction() {
  document.getElementById("myCheck").required = true;
  document.getElementById("demo").innerHTML = "The required property was set.";
}
</script>

</body>
</html>



PreviousNext

Related