René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Checking state of radio buttons with JavaScript

<html>
<head>

<script>
  function alert_which_radio_button(f) {

    if (f.val[0].checked) alert("you chose 1");
    if (f.val[1].checked) alert("you chose 2");
    if (f.val[2].checked) alert("you chose 3");
    if (f.val[3].checked) alert("you chose 4");
    if (f.val[4].checked) alert("you chose 5");
  }
</script>

</head>
<body>

<form name='frm'>
      <input name='val' type='radio' value='1' checked> 1
  <br><input name='val' type='radio' value='2'> 2
  <br><input name='val' type='radio' value='3'> 3
  <br><input name='val' type='radio' value='4'> 4
  <br><input name='val' type='radio' value='5'> 5

  <p><input type='button' value='check' onClick='alert_which_radio_button(frm);'>

</form>


</body>
</html>