faqts : Computers : Programming : Languages : JavaScript : Forms : Radio buttons

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

104 of 117 people (89%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How can I uncheck a radio button group?

Mar 20th, 2000 03:43
Martin Honnen,


Only one radio button in a group can be checked so you just have to 
loop through to find that button and change its checked property.
The following contains a function doing it plus two examples applying 
the function, the case of the group of consisting of just one button is 
covered:
<HTML>
<HEAD>
<SCRIPT>
function uncheckRadioGroup (radioButtonOrGroup) {
  if (radioButtonOrGroup.length) { // we have a group
    for (var b = 0; b < radioButtonOrGroup.length; b++)
      if (radioButtonOrGroup[b].checked) {
        radioButtonOrGroup[b].checked = false;
        break;
      }
  }
  else 
    radioButtonOrGroup.checked = false;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="formName">
<INPUT TYPE="radio" NAME="aRadioGroup" VALUE="Kibo">
<INPUT TYPE="radio" NAME="aRadioGroup" VALUE="Xibo">
<INPUT TYPE="radio" NAME="aRadioGroup" VALUE="Maho">
<INPUT TYPE="button" VALUE="uncheck"
       ONCLICK="uncheckRadioGroup(this.form.aRadioGroup)"
>
<BR>
<INPUT TYPE="radio" NAME="aRadio" VALUE="Kibo">
<INPUT TYPE="button" VALUE="uncheck"
       ONCLICK="uncheckRadioGroup(this.form.aRadio)"
>
</FORM>
</BODY>
</HTML>