Please find the following code. In function setAnswers, I am looping with each question. I have inner loop, which is looping for each answers of the questions. If any of the answer for question 1 and question2 is selected this code is working fine. But if None of answer of either question selected, it is not iterating through outer loop. Please help me to point out the error.
Expand|Select|Wrap|Line Numbers
- <html>
- <head>
- <style type="text/css">
- body {background-color: #CC9999}
- </style>
- <title>Test Questions</title>
- </head>
- <script language="Javascript">
- function setAnswers(callout_form) {
- var counter = document.getElementById("Ques_num").value;
- alert("What is in couneter..." + counter);
- for (var cnt=1; cnt<=counter; cnt++) {
- alert("Hey");
- alert("document.forms[0].choice" + cnt);
- var myChoice = eval("document.forms[0].choice" + cnt);
- alert(myChoice.length);
- for (g=0; g<=myChoice.length; g++) {
- //alert(myChoice[g].value);
- //alert((myChoice[g]).checked);
- if((myChoice[g]).checked) {
- break;
- }
- }
- }
- }
- </script>
- <body>
- <hr/>
- Steps:1
- <hr/>
- <form name="Callout_form" method="post" onsubmit="setAnswers(this);">
- <input type=hidden name="Ques_num" value=2 />
- <ol>
- <br><br><li>What is your favotite color?<br><input type="radio" name=choice1 value=red > Red
- <br><input type="radio" name=choice1 value=blue > Blue
- <br><input type="radio" name=choice1 value=pink > Pink
- <br><input type="radio" name=choice1 value=black > Black
- <br><input type="radio" name=choice1 value=purple > Purple
- <br><input type="radio" name=choice1 value=white > White
- <br><input type="radio" name=choice1 value=yellow > Yellow
- <br><br><br><li>What kind of drink do your preferend?
- <br><input type="checkbox" name=choice2 value=coke > Coke
- <br><input type="checkbox" name=choice2 value=orange_juice > Orange Juice
- <br><input type="checkbox" name=choice2 value=pepsi > Pepsi
- <br><input type="checkbox" name=choice2 value=sunkist > Sunkist
- <br><input type="checkbox" name=choice2 value=ginger_ale > Ginger Ale
- <br><input type="checkbox" name=choice2 value=Choc_milk > Choc Milk
- <br><input type="checkbox" name=choice2 value=coffee > Coffee
- <br><input type="checkbox" name=choice2 value=green_tea > Green Tea
- <br></li></ol>
- <hr/>
- <div id="form">
- <input type="submit" class="button" value="CONTINUE"/>
- <input type="button" class="button" onclick="window.close();" value="CANCEL" />
- </form>
- </body>
- </html>