17 Replies Latest reply on Feb 5, 2016 6:46 PM by stealthrs

    Javascript to display output if multiple checkboxes are selected

    tbrookes1980 Level 1

      Hello

       

      My PDF form has a number of question on it, some of which are multiselect;

       

      e.g.

       

      Identify the colours in our company logo (there are three)

      Blue

      Red

      Green

      Orange

      Yellow

      Purple

       

      If they select the correct ones (blue, red and yellow), the script should show an output of "Correct", if they pick anything else, it should show incorrect. I've worked out how to do this for a radio button grouping but can't figure out checkboxes.

       

      Any help would be greatly appreciated.

       

      Regards

        • 1. Re: Javascript to display output if multiple checkboxes are selected
          try67 MVP & Adobe Community Professional

          You can do something like this as the custom calculation script of the text field:

           

          event.value = (this.getField("Blue").isBoxChecked(0)==true && this.getField("Red").isBoxChecked(0)==true && this.getField("Yellow").isBoxChecked(0)==true && this.getField("Green").isBoxChecked(0)==false && ...) ? "Correct" : "Incorrect";
          
          • 2. Re: Javascript to display output if multiple checkboxes are selected
            tbrookes1980 Level 1

            Thankyou for your help. I tried to copy your script above and came up with this:

             

            event.value = (this.getField("Question2b").isBoxChecked(0)==true && this.getField("Question2c").isBoxChecked(0)==true && this.getField("Question2e").isBoxChecked(0)==true && this.getField("Question2f").isBoxChecked(0)==true && this.getField("Question2a").isBoxChecked(0)==false && this.getField("Question2a").isBoxChecked(0)==false && this.getField("Question2d").isBoxChecked(0)==false);? "Correct" : "Incorrect"; 

             

            but it doesn't work - I'm sure there's an obvious error but I can't figure it out. Any advice?

            • 3. Re: Javascript to display output if multiple checkboxes are selected
              try67 MVP & Adobe Community Professional

              Remove the semi-colon you added after the condition.

              • 4. Re: Javascript to display output if multiple checkboxes are selected
                tbrookes1980 Level 1

                Sorry to keep bothering you...I am really new to javascript. I have removed the semicolon but when I preview the form and select the correct answers, I don't get any output in that field. The script I have is:

                 

                event.value = (this.getField("Question2b").isBoxChecked(0)==true && this.getField("Question2c").isBoxChecked(0)==true && this.getField("Question2e").isBoxChecked(0)==true && this.getField("Question2f").isBoxChecked(0)==true && this.getField("Question2a").isBoxChecked(0)==false && this.getField("Question2a").isBoxChecked(0)==false && this.getField("Question2d").isBoxChecked(0)==false)? "Correct" : "Incorrect";

                 

                Could you kindly provide any further assistance?

                 

                Thankyou

                • 5. Re: Javascript to display output if multiple checkboxes are selected
                  George_Johnson MVP & Adobe Community Professional

                  Check to see if there are any errors in the JavaScript console by pressing Ctrl+J and if is says anything, report what is says verbatim. Also, you have this,getField("Question2a") in there twice.

                  • 6. Re: Javascript to display output if multiple checkboxes are selected
                    George_Johnson MVP & Adobe Community Professional

                    You should also replace "Checkbox2a" etc. with the actual names of the checkboxes. Case matters, so checkbox2a is not the same as CheckBox2A.

                    • 7. Re: Javascript to display output if multiple checkboxes are selected
                      tbrookes1980 Level 1

                      Thankyou George_Johnson, I have removed the duplicate Question 2a. so my script now looks like this:

                       

                      event.value = (this.getField("Question2b").isBoxChecked(0)==true && this.getField("Question2c").isBoxChecked(0)==true && this.getField("Question2e").isBoxChecked(0)==true && this.getField("Question2f").isBoxChecked(0)==true && this.getField("Question2a").isBoxChecked(0)==false && this.getField("Question2d").isBoxChecked(0)==false)? "Correct": "Incorrect"

                       

                      I have also confirmed that I have the right case for each of the checkboxes. They are called :

                       

                      Question 2a

                      Question 2b

                      Question 2c

                      Question 2d

                      Question 2e

                      Question 2f

                      I also pressed CTRL+J but didn't get any error messages.

                       

                      I'm really not sure what else to do...

                      • 8. Re: Javascript to display output if multiple checkboxes are selected
                        George_Johnson MVP & Adobe Community Professional

                        Did you place this as a custom calculation script?

                        • 9. Re: Javascript to display output if multiple checkboxes are selected
                          tbrookes1980 Level 1

                          Yes, I went to Properties>Calculate> Custom Calculation Script> Edit

                           

                          Then I pasted in the below.... I'm not sure what's missing..

                           

                           

                          event.value = (this.getField("Question2b").isBoxChecked(0)==true && this.getField("Question2c").isBoxChecked(0)==true && this.getField("Question2e").isBoxChecked(0)==true && this.getField("Question2f").isBoxChecked(0)==true && this.getField("Question2a").isBoxChecked(0)==false && this.getField("Question2d").isBoxChecked(0)==false)? "Correct": "Incorrect"

                          • 12. Re: Javascript to display output if multiple checkboxes are selected
                            stealthrs Level 1

                            George,

                             

                            I tried to search what I'm looking for, and I feel this is mostly close to what I'm trying to do...Say I have 2 checkboxes like in your format (Question2a and Question2b)... If ONE them between the two is answered "Yes", I want the third checkbox (not textfield) to automatically populate YES...  If they are both answered as "No", then I want the third checkbox (Question2c) to be NO as well.

                             

                            I understand how to do the checkbox names & export values... I'm just lacking the code know how.

                             

                            Edit: I tried to write my own code to figure this out, and I can get my concept to work with a TextBox, but not a checkbox... Here's how far I got

                             

                            event.value = (this.getField("Question2a").isBoxChecked(0)==true || this.getField("Question2b").isBoxChecked(0)==true) ? "Yes": "No"

                             

                            How do I get this code to tell the checkbox Question2c to be checked if Question2a and Question2b are checked?

                            • 13. Re: Javascript to display output if multiple checkboxes are selected
                              stealthrs Level 1

                              I tried it another way if what's above isn't close.  I'm getting warm; but, I have to issues.... 1) I'm running into a syntax error and I don't know how to fix it... and 2) I did this as a JavaScript on Question2a without variable h to get the checkbox to copy down... Then in re-writing it this doesn't work... I'm thinking I need to do a custom calcuation script on Question2c to make this work more reliably?? I'm honestly not sure

                               

                              var f = this.getField("Question2a");

                              var g = this.getField("Question2b");

                              var h = this.getField("Question2c");

                               

                              // test the field CheckBoxFirst to see if it is checked

                               

                              if(f.isBoxChecked(0)) || if(g.isBoxChecked(0))        // 0 = the box is checked

                                  h.checkThisBox(0,true);                           // true = check this box

                              else

                                  h.checkThisBox(0,false);                          // false = don't check box

                              • 14. Re: Javascript to display output if multiple checkboxes are selected
                                try67 MVP & Adobe Community Professional

                                To solve the syntax error replace the if-line with this:

                                 

                                if (f.isBoxChecked(0) || g.isBoxChecked(0))

                                • 15. Re: Javascript to display output if multiple checkboxes are selected
                                  stealthrs Level 1

                                  I dunno where I would be without you and George... Thanks to you both. 

                                   

                                  The final working code for this is

                                   

                                  var f = this.getField("Question2a");

                                  var g = this.getField("Question2b");

                                  var h = this.getField("Question2c");

                                   

                                  // test the field CheckBoxFirst to see if it is checked

                                    

                                  if(f.isBoxChecked(0) || g.isBoxChecked(0))            // 0 = the box is checked

                                      h.checkThisBox(0,true);                           // true = check this box

                                  else

                                      h.checkThisBox(0,false);                          // false = don't check box

                                   

                                   

                                  To those who are reading/learning this stuff: please disregard what I wrote above about "I'm thinking I need to do a custom calcuation script on Question2c to make this work more reliably?? I'm honestly not sure" because you can't do this for checkboxes. You have to add this code as JavaScript for Question2a and Question2b checkboxes.

                                  • 16. Re: Javascript to display output if multiple checkboxes are selected
                                    stealthrs Level 1

                                    Eugh... I'm having trouble again... (and I'm reposting this same post, because it's been a week and I realized I replied to the wrong person)

                                     

                                    Try67 or George...

                                     

                                    What if I wanted to have Question2a, Question2b, Question2c to be Yes/No boxes? So that's a total of 6 boxes.... One column exports YES and the other column exports NO

                                     

                                    Question2d has two checkboxes too... If Question2a, Question2b, or Question2c have an answer of YES, then Question2d gets a YES.  So if there's just one single YES checked, then Question2d will get checked YES.


                                    If and only if Question2a, Question2b, Question2c are all NO, then Question2d gets a NO. Otherwise, I'd like it to be blank.


                                    How can I get this done?  I tried to manipulate the code I have to do it; but, I don't know how the 0's, 1's, true's and false's are supposed to work...

                                     

                                    Here's my baseline code (THAT DOESN'T WORK) so far

                                     

                                    var f = this.getField("Question2"a);

                                    var g = this.getField("Question2b");

                                    var h = this.getField("Question2c");

                                    var i = this.getField("Question2d");

                                     

                                    // test the field CheckBoxFirst to see if it is checked

                                     

                                    if(f.isBoxChecked(0) || g.isBoxChecked(0) || h.isBoxChecked(0))            // 0 = the box is checked

                                        i.checkThisBox(Yes,true);                           // true = check the 'Yes' box

                                        i.checkThisBox(No,false);                        

                                    else

                                        i.checkThisBox(Yes,false);

                                        i.checkThisBox(No,true);

                                    • 17. Re: Javascript to display output if multiple checkboxes are selected
                                      stealthrs Level 1

                                      Boom!  Figured it out.  When you make a checkbox that exports a particular value, it will give you a specific referral number for that answer (export 'Yes' is #0 and export 'No' is #1.  You can figure out the number it's assigned by pulling open the Fields window when you're editing forms.  You will see some order of the following (mostly sorted by tab order):

                                       

                                      Questions2a#0

                                      Questions2b#0

                                      Questions2c#0

                                      Questions2d#0

                                      Questions2a#1

                                      Questions2b#1

                                      Questions2c#1

                                      Questions2d#1

                                      Questions2e#0

                                      Questions2e#1

                                       

                                      Correct code is:

                                       

                                      var f = this.getField("Question2a");

                                      var g = this.getField("Question2b");

                                      var h = this.getField("Question2c");

                                      var i = this.getField("Question2d");

                                      var j = this.getField("Question2e");

                                       

                                       

                                      // test the field CheckBoxFirst to see if it is checked - '1' checks whether the checkboxes with 'No' Exports are checked

                                      // true = checked box, false = don't check the box

                                         

                                      if(f.isBoxChecked(0,true) || g.isBoxChecked(0,true) || h.isBoxChecked(0,true) || i.isBoxChecked(0,true)) // Check if any 'Yes' boxes are checked

                                          j.checkThisBox(0,true);                         // Tells the 'Yes' checkbox to be checked

                                       

                                       

                                      if(f.isBoxChecked(1,true) && g.isBoxChecked(1,true) && h.isBoxChecked(1,true) && i.isBoxChecked(1,true)) // Check if all 'No' boxes are checked

                                          j.checkThisBox(1,true);                         // Tells the 'No' checkbox to be checked

                                       

                                       

                                      else                                                

                                          j.checkThisBox(1,false);                           // tell box series 'Yes' to check itself