Connecting Tech Pros Worldwide Help | Site Map

Problem with checkbox looping

Newbie
 
Join Date: Jul 2008
Posts: 14
#1: Aug 27 '08
Hi,
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
  1. <html> 
  2. <head>
  3. <style type="text/css">
  4. body {background-color: #CC9999}
  5. </style>
  6. <title>Test Questions</title>
  7. </head>
  8. <script language="Javascript">
  9. function setAnswers(callout_form) {
  10.     var counter = document.getElementById("Ques_num").value;
  11.     alert("What is in couneter..." + counter);
  12.  
  13.     for (var cnt=1; cnt<=counter; cnt++) {
  14.         alert("Hey");
  15.         alert("document.forms[0].choice" + cnt);
  16.         var myChoice = eval("document.forms[0].choice" + cnt);
  17.         alert(myChoice.length);
  18.           for (g=0; g<=myChoice.length; g++) {
  19.             //alert(myChoice[g].value);
  20.             //alert((myChoice[g]).checked);
  21.             if((myChoice[g]).checked) {
  22.                 break;
  23.             } 
  24.  
  25.          }
  26.  
  27.     }
  28. }
  29.  
  30. </script>
  31. <body>
  32. <hr/>
  33. Steps:1
  34. <hr/>
  35. <form name="Callout_form" method="post" onsubmit="setAnswers(this);">
  36. <input type=hidden name="Ques_num" value=2 />
  37. <ol>
  38. <br><br><li>What is your favotite color?<br><input type="radio" name=choice1 value=red > Red
  39. <br><input type="radio" name=choice1 value=blue > Blue
  40. <br><input type="radio" name=choice1 value=pink > Pink
  41. <br><input type="radio" name=choice1 value=black > Black
  42. <br><input type="radio" name=choice1 value=purple > Purple
  43. <br><input type="radio" name=choice1 value=white > White
  44. <br><input type="radio" name=choice1 value=yellow > Yellow
  45. <br><br><br><li>What kind of drink do your preferend?
  46. <br><input type="checkbox" name=choice2 value=coke > Coke
  47. <br><input type="checkbox" name=choice2 value=orange_juice > Orange Juice
  48. <br><input type="checkbox" name=choice2 value=pepsi > Pepsi
  49. <br><input type="checkbox" name=choice2 value=sunkist > Sunkist 
  50. <br><input type="checkbox" name=choice2 value=ginger_ale > Ginger Ale
  51. <br><input type="checkbox" name=choice2 value=Choc_milk > Choc Milk
  52. <br><input type="checkbox" name=choice2 value=coffee > Coffee
  53. <br><input type="checkbox" name=choice2 value=green_tea > Green Tea
  54.  
  55. <br></li></ol>
  56.  
  57.         <hr/>
  58.         <div id="form">
  59.                 <input type="submit" class="button" value="CONTINUE"/>
  60.                 <input type="button" class="button" onclick="window.close();" value="CANCEL" />
  61.         </form>
  62. </body>
  63. </html>
  64.  
  65.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Aug 28 '08

re: Problem with checkbox looping


On this line:
Expand|Select|Wrap|Line Numbers
  1. for (g=0; g<=myChoice.length; g++) {
g loops from 0 to the length of myChoice which is 1 more than the number of radio buttons. Use < not <=.
Newbie
 
Join Date: Jul 2008
Posts: 14
#3: Aug 29 '08

re: Problem with checkbox looping


Thanks acoder,
I figured it out. I appreciate your kind help
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Aug 29 '08

re: Problem with checkbox looping


You're welcome. Glad it's working.
Reply