Link to home
Start Free TrialLog in
Avatar of moswa
moswa

asked on

check radiobutton

I would like to check if one of my radiobuttons is checked.
By default, NONE of the radio-buttons must be pre-checked
Problem: with my currently script, it checkes which value is checked, BUT if none of the radio buttons is checked, it gives automatically the value of the first radio button.

<radio name=a1 value=1>
<radio name=a1 value=2>
<radio name=a1 value=3>
<radio name=a1 value=4>
<radio name=a1 value=5>


function getSelectedButton(buttonGroup)
  {
  for (var i=0;i<buttonGroup.length;i++)
    {
    if (buttonGroup[i].checked)
     {
     return i
        }
    }
  return 0
  }

var i=getSelectedButton(document.form.a1)
alert("You chose "+ form.a1[i].value + ".")
ASKER CERTIFIED SOLUTION
Avatar of CJ_S
CJ_S
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of moswa
moswa

ASKER

thanx,

moswa
your function returns 0 :

function getSelectedButton(buttonGroup){
 for (var i=0;i<buttonGroup.length;i++){
   if (buttonGroup[i].checked) {
    return i
   }
 }

// this statement makes it zero
 return 0
}

u need to remove that part.... :^) third