Javascript DOM HTML Input Radio check and uncheck

Introduction

Check and uncheck a specific radio button:

View in separate window

<!DOCTYPE html>
<html>
<body>

<form>
  What color do you prefer?<br>
  <input type="radio" name="colors" id="red">Red<br>
  <input type="radio" name="colors" id="blue">Blue
</form>// w  ww.  j a  va 2 s .  co  m

<button onclick="check()">Check "Red"</button>
<button onclick="uncheck()">Uncheck "Red"</button>

<script>
function check() {
  document.getElementById("red").checked = true;
}
function uncheck() {
  document.getElementById("red").checked = false;
}
</script>

</body>
</html>

The checked property sets or gets the checked state of a radio button.

This property mirrors the HTML checked attribute.

Value Description
true The radio button is checked
false Default. The radio button is not checked

The checked property returns true if the radio button is checked, and false if the radio button is not checked.




PreviousNext

Related