Javascript Form How to - Get Radio button's checked state








Question

We would like to know how to get Radio button's checked state.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
function show() {<!--from w  w w  .j a  v  a 2s.c o m-->
       console.log(document.getElementById("inp1").checked + " " + document.getElementById("inp2").checked);
}

</script>
</head>
<body>
  <input type="radio" id="inp1" name="same" onclick="show();" />
  <input type="radio" id="inp2" name="same" onclick="show();" />
</body>
</html>

The code above is rendered as follows: