Link to home
Start Free TrialLog in
Avatar of rgarimella
rgarimella

asked on

Multiple Radio Buttons Check

Hi Folks,

I have a series of radio button group with yes/no options.

I want to evaluate it to an array of correct answers, how do I do it?

var correctanswers = [1,0,1,0];

Where 1 is true or Yes is selected and 0 is false or No is selected

This has to be dynamic as sometimes there will be more than 4

Basically I want to match the arrays, if it is a correct match or not

Any help is appreciated
Thanks

RG
<form name="mcForm" method="post"> 
  <table width="300" border="0" cellspacing="0" cellpadding="4">
      <tr>
          <td width="26" valign="top" id="radio1"> <input type="radio" name="radiobutton1" value="yes" accesskey="A"  id="yes"></td>
          <td width="18" valign="top" id="radio1"><input type="radio" name="radiobutton1" value="no" accesskey="B"  id="no"></td>
          <td width="556" valign="top" id="Text1"><font size="2" face="Arial, Helvetica, sans-serif"><label for="1">They compare different  strategies</label></font></td>
      </tr>
      <tr>
          <td width="26" valign="top" id="radio2"> <input type="radio" name="radiobutton2" value="yes" accesskey="C"  id="yes"></td>
          <td width="18" valign="top" id="radio2"><input type="radio" name="radiobutton2" value="no" accesskey="D"  id="no"></td>
          <td width="556" valign="top" id="Text2"><font size="2" face="Arial, Helvetica, sans-serif"><label for="2">They provide performance-based incentives
    </label></font></td>
	</tr>
      <tr>
          <td width="26" valign="top" id="radio3"> <input type="radio" name="radiobutton3" value="yes" accesskey="E"  id="yes"></td>
          <td width="18" valign="top" id="radio3"><input type="radio" name="radiobutton3" value="no" accesskey="F"  id="no"></td>
          <td width="556" valign="top" id="Text3"><font size="2" face="Arial, Helvetica, sans-serif"><label for="3">They provide data 
    </label></font></td>
	</tr>
      <tr>
          <td width="26" valign="top" id="radio4"> <input type="radio" name="radiobutton" value="yes" accesskey="G"  id="yes"></td>
          <td width="18" valign="top" id="radio4"><input type="radio" name="radiobutton" value="no" accesskey="H"  id="no"></td>
          <td width="556" valign="top" id="Text4"><font size="2" face="Arial, Helvetica, sans-serif"><label for="4">They demonstrate the benefits 
    </label></font></td>
</tr>
 
      <tr> 
        <td>&nbsp;</td>
        <td></td>
        <td> <div align="left"> 
            <input type="button" value="Check Answer" onClick="checkAnswer(form)">
        </div></td>
      </tr>	
  </table>
</form>

Open in new window

Avatar of humanonomics
humanonomics
Flag of India image

Try something like given in the attached snippet.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
<head>
<title></title>
        <style type="text/css"><!--
        #section1,#section2,#section3{color:#dddddd;}
        --></style>
 
<script type="text/javascript">
function checkAnswer()
{
var al = new Array();
 
for(var i = 1; i <= document.getElementById('TotalNumOfRDButton').value; i++)
{
alert(document.getElementsByName("radiobutton" + i)[0].checked);
if(document.getElementsByName("radiobutton" + i)[0].checked)
{
al[i] = 1;
}
else
{
al[i] = 0;
}
 
 
}
 
for(var i = 1; i <= document.getElementById('TotalNumOfRDButton').value; i++)
{
alert(" >>" + al[i]);
}
 
}
</script>
</head>
<body>
<form name="mcForm" method="post"> 
 
  <table width="300" border="0" cellspacing="0" cellpadding="4">
      <tr>
          <td width="26" valign="top" id="radio1"> <input type="radio" name="radiobutton1" value="yes" accesskey="A"  id="yes"></td>
          <td width="18" valign="top" id="radio1"><input type="radio" name="radiobutton1" value="no" accesskey="B"  id="no"></td>
          <td width="556" valign="top" id="Text1"><font size="2" face="Arial, Helvetica, sans-serif"><label for="1">They compare different  strategies</label></font></td>
      </tr>
      <tr>
          <td width="26" valign="top" id="radio2"> <input type="radio" name="radiobutton2" value="yes" accesskey="C"  id="yes"></td>
          <td width="18" valign="top" id="radio2"><input type="radio" name="radiobutton2" value="no" accesskey="D"  id="no"></td>
          <td width="556" valign="top" id="Text2"><font size="2" face="Arial, Helvetica, sans-serif"><label for="2">They provide performance-based incentives
    </label></font></td>
        </tr>
      <tr>
          <td width="26" valign="top" id="radio3"> <input type="radio" name="radiobutton3" value="yes" accesskey="E"  id="yes"></td>
          <td width="18" valign="top" id="radio3"><input type="radio" name="radiobutton3" value="no" accesskey="F"  id="no"></td>
          <td width="556" valign="top" id="Text3"><font size="2" face="Arial, Helvetica, sans-serif"><label for="3">They provide data 
    </label></font></td>
        </tr>
      <tr>
          <td width="26" valign="top" id="radio4"> <input type="radio" name="radiobutton4" value="yes" accesskey="G"  id="yes"></td>
          <td width="18" valign="top" id="radio4"><input type="radio" name="radiobutton4" value="no" accesskey="H"  id="no"></td>
          <td width="556" valign="top" id="Text4"><font size="2" face="Arial, Helvetica, sans-serif"><label for="4">They demonstrate the benefits 
    </label></font></td>
</tr>
 
      <tr> 
        <td> </td>
        <td></td>
        <td> <div align="left"> 
            <input type="button" value="Check Answer" onClick="checkAnswer()">
        </div></td>
      </tr>     
  </table>
  <INPUT TYPE="HIDDEN" VALUE="4" ID='TotalNumOfRDButton'>
</form>
</body>
</html>

Open in new window

Avatar of rgarimella
rgarimella

ASKER

What does this line do

<INPUT TYPE="HIDDEN" VALUE="4" ID='TotalNumOfRDButton'>


The value will be dynamic, sometimes there will be 4 sets of radio buttons and then sometimes it is 5 or 6 etc.

how are you going to generate them dynamically? by using Javascript ?
No, the distractors are generated dynamically by ASP

so I dont know the number of distractors before hand
is it gauranteed that ids would always be radio1 radio2 and so on ?
ASKER CERTIFIED SOLUTION
Avatar of MatthiasVance
MatthiasVance
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
Sorry you need to replace line 11 with this code to get the result you (hopefully) want.

Kind regards,

Matthias Vance
answers[answers.length] = (input.value.toLowerCase() == "yes" ? 1 : 0);

Open in new window