13 Replies Latest reply on Feb 6, 2012 4:13 PM by try67

    Totaling Radio Check boxes

    hamRhino

      Hello,

       

      I've been struggling through this for about a day, and I'll be the first to admit that my Javascript is not that great...anyway here goes:

       

      I have a form with a checkbox grid (4 accross and 13 down.)

       

       

      Column 1Column 2Column 3Column 4
      x---------
      ------x---
      ------x---
      x---------
      ---------x
      ------------
      ------------
      ------------
      ------------
      ------------
      ------------
      ------------
      ------------

       

      The rows allow a maximum of one entry per row (which i built making all of the checkboxes the same name...i.e. classify[1] for  the first row checkboxes, classify[2] for the second, etc.). Also, each checkbox column has a different export value (all checkboxes in column 1 have an export value of 0, column 2 = 1, etc.). Btw, I didn't go with radio buttons because of the inherent 'stickiness' of not being able to uncheck a checked row.

       

      My intent is to tally up the amount of boxes checked in each column. In the example above, it should return a value of 2 for the first column.  I'm using a calculate script for the first column total that goes something like this:

       

      var i=0; tot0=0; for (i=0; i<=13; i++) { if (classify[i].value == '0') {   tot0=tot0+1; }; }; this.getField('totChoice1')=tot0;

       

      Obviously the script isn't doing what I intended or I wouldn't be posting here . So I need a bit of help.

       

      Thanks a bunch,

      Ryne

        • 1. Re: Totaling Radio Check boxes
          try67 MVP & Adobe Community Professional

          Are the names of your fields actually "classify[1]", "classify[2]", etc.? If so, then you can use this script as the custom calculation code of your total field:

           

          var tot0=0;

          for (var i=0; i<=13; i++) {

              if (this.getField("classify[" + i + "]").value == '0') {  

                  tot0++;

              }

          }

          event.value = tot0;

          1 person found this helpful
          • 2. Re: Totaling Radio Check boxes
            hamRhino Level 1

            Yes, I assumed that they would be predefined as an array with varying positions being the rows if i typed them up as so...evidently not.

             

            The solution didn't seem to work when I put it in the "Custom Calculation script" box for the totChoice1 textbox. Do i need to add a trigger to each box?

            • 3. Re: Totaling Radio Check boxes
              try67 MVP & Adobe Community Professional

              Are there any error message in the JS console? Make sure to tick Show

              Console on Errors and Warnings under Edit - Prefs. - JavaScript.

              You don't need to add triggers to the boxes. The calculation script will

              execute after each time a value changes anywhere in the file.

              • 4. Re: Totaling Radio Check boxes
                hamRhino Level 1

                Thanks, that will definitely come in handy! Here's what it spit back:

                 

                ReferenceError: classify is not defined

                3:Field:Calculate

                TypeError: this.getField("classify[" + i + "]") is null

                3:Field:Calculate

                TypeError: this.getField("classify[" + i + "]") is null

                3:Field:Calculate

                 

                Strange, it doesn't seem to like the export value if  I'm reading this correctly...is it because the export value is 0?

                • 5. Re: Totaling Radio Check boxes
                  try67 MVP & Adobe Community Professional

                  It's because one of the field names is not correct. Double-check your

                  fields. Make sure there's not an extra space somewhere. You can also add

                  some debugging lines, for example you can print the value of i to the

                  console to see where it goes wrong.

                  • 6. Re: Totaling Radio Check boxes
                    try67 MVP & Adobe Community Professional

                    I just noticed that i starts from 0, but you said your fields are called "classify1", etc. So i should start from 1, not 0.

                    • 7. Re: Totaling Radio Check boxes
                      hamRhino Level 1

                      Yep that did it! Works like a charm.

                       

                      Thank you so much!

                      • 8. Re: Totaling Radio Check boxes
                        wampsher

                        I have a similar problem. I have 4 columns (25 questions) with radio buttons with the same button name for each row (first row of 4 buttons is named radiobutton1, second row is radiobutton2, etc). I used radio buttons so that only one selection can be made in each row. I need a total at the bottom of each column but I can't figure out the script for this. The columns are named Agree, SomewhatAgree, SomewhatDisagree, Disagree. I tried changing the script from above but I have no understanding of what needs to go where and I am stumped. Any ideas?

                        • 9. Re: Totaling Radio Check boxes
                          try67 MVP & Adobe Community Professional

                          If you set an export value to each radio button (like 1 for Agree, 2 for

                          Somewhat Agree, etc.) then you don't need any scripts. Just set the total

                          field to be the sum of those fields, using the first option in the

                          Calculate tab.

                          • 10. Re: Totaling Radio Check boxes
                            wampsher Level 1

                            I must not be understanding this. If I set the export value (which I believe is the same as the button value) to 1 then I lose the button value name of Agree, Disagree, etc. so then any selection made on the radio buttons ends up being calculated in all four columns rather than the column in which the selection was made. Am I misunderstanding where to put the export value?

                            • 11. Re: Totaling Radio Check boxes
                              try67 MVP & Adobe Community Professional

                              The export value can be found under the Options tab of the Properties of

                              each radio button.

                              • 12. Re: Totaling Radio Check boxes
                                wampsher Level 1

                                I have the button value (aka export value) set at 1 but I can't total each column individually. Setting the export value to 1 for all my radio buttons results in the same total for all four columns; when I select a radio button in one of the four columns it ends up in the total for all four columns. Does that make sense. I must be misunderstanding something really simple...

                                • 13. Re: Totaling Radio Check boxes
                                  try67 MVP & Adobe Community Professional

                                  If you wish, send me the file to try6767 at gmail dot com and I'll have a

                                  look at what's going on...