473,320 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem with checkbox looping

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.  
Aug 27 '08 #1
3 1926
acoder
16,027 Expert Mod 8TB
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 <=.
Aug 28 '08 #2
Thanks acoder,
I figured it out. I appreciate your kind help
Aug 29 '08 #3
acoder
16,027 Expert Mod 8TB
You're welcome. Glad it's working.
Aug 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Fabri | last post by:
How can I, on button click, to select ONLY the following 4 checkbox? I would like to do this without a for loop through form.lenght because this is only an example and I have to apply this script...
1
by: Stephan Bour | last post by:
I was playing with this simple code strait from MSDN and I got the following compile error: ³foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.DataList' because...
0
by: Stu Lock | last post by:
Hi, I have a data grid with a checkbox column in it bound to database. (IsItNumeric return either True/False). <asp:TemplateColumn HeaderText="In List"> <ItemTemplate> <asp:CheckBox...
2
by: wrytat | last post by:
I have a datagrid that has a column of checkbox in every row. And also 2 columns of 2 forms, each form with a textarea and a submit button. I have a button below the datagrid. And I place a form...
4
by: Bauer | last post by:
I have a datagrid whose rows contain checkbox and hyperlink columns(containing employee_id field retireved from database). Once the user checks the checkboxes i need to get the employee ids whose...
34
by: clinttoris | last post by:
Hello Experts, I have been told to post this in the Javascript forum as I want to do this client side just before my form gets submitted. Once the user clicks the submit button a javascript...
7
by: aurora10 | last post by:
Hi, I have textfile with True or Flase in it and I need to read this file so that the value is assigned to a checkbox on the form. How can I do that ? Thanks:)
4
by: BA | last post by:
Hello, I have a very strange code behavior that I cannot make heads or tails of: I have c# code being executed in BizTalk assemblies which is acting very strangely. In my BizTalk process I...
1
by: =?Utf-8?B?RG90TmV0RGV2?= | last post by:
Hi, Does any one have an example of using Checkboxes in Datagrid? I want to have postback enabled for each single checkbox and then execute the code accordingly. Instead of having a button and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.