Javascript DOM HTML Input Radio required Property get

Introduction

Find out if a radio button must be checked before submitting a form:

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

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

View in separate window

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Radio Button: <input type="radio" id="myRadio" name="test" required>
  <input type="submit">
</form>//from   w  ww  . j a  va2  s . c  o  m

<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

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

This property mirrors the HTML required attribute.

The required property accepts and returns a boolean type value.

Value Description
true The radio button must be checked before submitting a form
false Default. The radio button is not a required part of form submission

The required property returns true if the radio button must be checked before submitting a form, otherwise it returns false




PreviousNext

Related