Radio Button Test




<SCRIPT>

function testButton(form) {
     for (Count = 0; Count < 3; Count++) {
          if (form.test[Count].checked) break// when the radio button is found drop out of the for loop
     }
     alert ("document.testform.test[ " + Count + "] is selected");
}

</SCRIPT>

<FORM NAME="testform">
     <INPUT TYPE="radio" NAME="test" VALUE="rdoBtn1">
     <INPUT TYPE="radio" NAME="test" VALUE="rdoBtn2">
     <INPUT TYPE="radio" NAME="test" VALUE="rdoBtn3">
     <INPUT TYPE="button" VALUE="Click" onClick="testButton(this.form)">
</FORM>

The for loop in the testButton() cycles through all of the buttons in the "test" group. When it finds the button that's selected, it breaks out of the loop and displays the button number (remember: starting from 0).

Setting a radio button selection with HTML is simple. If you want the form to initially appear with a given radio button selected, add the CHECKED attribute to the HTML for that button:

<INPUT TYPE="radio" NAME="test" VALUE="rdoBtn1" CHECKED>

You can also set the button selection programmatically with JavaScript, using the CHECKED Property. Specify the index of the radio button array you want to checked.

form.test[0].checked = true;   // sets to first button in the test group