Skip to content Skip to sidebar Skip to footer

How To Have A Radio Button Select Another Radio Button's Value

I was wondering if there is a way to make a radio button select another radio buttons data. For example, a 'select all' radio button, would select another radio button. Or In the e

Solution 1:

HTML

<button type='button' onclick='selectAs0()'>Select All As 0</button>

<inputtype='radio' name='setA' value='0' /> 0
<inputtype='radio' name='setA' value='1' /> 1

<inputtype='radio' name='setB' value='0' /> 0
<inputtype='radio' name='setB' value='1' /> 1

JS

functionselectAs0() {
  varALL = document.getElementsByTagName("INPUT");

  for(var x = 0; x< ALL.length;x++) {
    if(ALL[x].type=='radio' && ALL[x].value == 0) {
      ALL[x].checked = true;
    }
  }
}

Untested at this point but should work-ish.

// EDIT

If you're determined to use a radio button to actuate the function, the same concept will work...

<inputtype='radio' onclick='selectAs0()' />

...but you'll have to add extra code to de-check the radio once it's already selected.

Solution 2:

Without doing all the work for you, the basic idea would be to have the "select all 0" button (or checkbox, as ThiefMaster said) with an onchange attribute set to a javascript routine, which then requests document.getElementsByTagName("input"), looping through the returned list and setting the "checked" attribute of the zero-valued radio-buttons. I can elaborate if necessary, but that ought to get you started.

Post a Comment for "How To Have A Radio Button Select Another Radio Button's Value"