4 Replies Latest reply on Jul 14, 2014 10:48 AM by MatLac

    How do I add variables to different checkboxes without using the 'Export Value' and then add them together (if checked)?

    RobynDahl

      Hello,

       

      I am very new to JavaScript.  I would like to create a checkbox with a numerical value variable activated if the box is checked.  Unfortunately, I cannot use the 'Export Value' because I already need to use it for something else.

       

      Any help is really appreciated.

       

      Thanks,

       

      Robyn

        • 1. Re: How do I add variables to different checkboxes without using the 'Export Value' and then add them together (if checked)?
          try67 MVP & Adobe Community Professional

          A check-box can have only 1 export value. You will need to write a code that checks if the box is ticked, using the isBoxChecked method or the value property, and then base your calculation off of that.

          • 2. Re: How do I add variables to different checkboxes without using the 'Export Value' and then add them together (if checked)?
            MatLac Level 1

            If you need a second export value, and a numerical value is the best one, you can trick your checkboxes by using a part of their name as the second export value.  For example, if you have checkBox.1 (export value = A) and checkBox.2 (export value = B), you can use something like this:

             

            if (event.target.isBoxChecked(0) == true){

                 var exportValue = event.value;

                 var secondExportValue = Number(event.target.name.replace("checkBox.", ""));

            }

             

            What I did was checking if the box is checked and if so, I placed it's value in a variable.  Then, I got the name of the box and replaced the first part of it with an empty value leaving only the number as a string.  Finally, placing it in Number() will turn it to number for you to make calculations via a second variable.  In this example, all numbers need to be different as the names should be different.

             

            Take note that this is probably not the best way to achieve what you are trying to do.  If you provide more information about what you intend to do with the calculation, I bet there is a far more easier way.

            • 3. Re: How do I add variables to different checkboxes without using the 'Export Value' and then add them together (if checked)?
              RobynDahl Level 1

              Thanks for your time. 

               

              I am trying to simply create a registration form for a dance company.  The form has to accommodate several different users with each user having approximately 90 different classes to choose from via checkboxes.   So the two bits of information I need from each checkbox is the class name (example: Intermediate Ballet) and the number of hours each class meets for (example: 1.5).  I then need to combine each student's selected class names into a text box and calculate the number of hours each student will take and display that sum in a different text box.

               

              -Robyn

              • 4. Re: How do I add variables to different checkboxes without using the 'Export Value' and then add them together (if checked)?
                MatLac Level 1

                Brace yourself, here's your solution.

                 

                First of all, the export value of you checkboxes should definitly be the number of hours for the class since you will just add them up further.

                Second, we will use the userName property for our second export value (its the field right under Name and its labeled "tooltip").  You will name the class there.

                Finally, you need to name each checkbox with a number at the end starting with 1 all the way to 190.  This will be used in loops. ("whatever.1", "whatever.2,....."whatever.190")

                 

                In the calculate event of "total hours":

                 

                var sum = 0;  //declaring a variable that will be used in the loop

                for (var i = 1; i <= 190; i++){    //this loop will scan through all your checkboxes

                     if (this.getField("whatever."+i).isBoxChecked(0) == true){  //if box is checked....

                          sum += Number(this.getField("whatever."+i).value);   //.....add its value to the sum

                     }

                }

                event.value = sum;  //display the total

                 

                 

                Now for the course name field, we will use the same pattern in the calculate event

                 

                var courseList = new Array();  //declaring an empty array that will contain the courses

                for (var i = 1; i <= 190; i++){    //this loop will scan through all your checkboxes

                     if (this.getField("whatever."+i).isBoxChecked(0) == true){  //if box is checked....

                          courseList.push(this.getField("whatever."+i).userName);   //.....add its userName (tooltip) to the array

                     }

                }

                event.value = "You are attending these courses:  "+courseList.join(", ")+".";  //display the array by turning it into a string and joining its items with ", "

                 

                You can try it out with just a few checkboxes at first.  Don't forget that the loop mustn't go further than the existing field or you will get an error and the script will stop.  Make sure no error show in the console (ctrl+J).