10 Replies Latest reply on Dec 23, 2013 8:17 AM by try67

    Checking all checkboxes

    rxraza01

      I am using Acrobat XI Pro to create fillable PDF

       

      I have a form with two checkboxes and I have a "check all" check box. What I need is when a user clicks on "check all" check box both check boxes need to checked

       

      for that purpose I put the following code in Mouse Up event of "checkall" check box but it does not appear to work

       

       

      if (event.target.value !== "Off") {

        this.getField("CheckBox1").value = "On";

       

        this.getField("CheckBox2").value = "On";

       

       

      }

      else

      {

      this.getField("CheckBox1").value = "Off";

        

      this.getField("CheckBox2").value = "Off";

         

      }

        • 1. Re: Checking all checkboxes
          George_Johnson MVP & Adobe Community Professional

          The default export value of a check box is "Yes", not "On", though it can of course be changed. So double check what it is and use that in the code.

          • 2. Re: Checking all checkboxes
            rxraza01 Level 1

            I tried 'Yes' and 'No' (assigned values) but that did not work out either

             

            if (event.target.value !== "No") {

              this.getField("CheckBox1").value = "Yes";

             

              this.getField("CheckBox2").value = "Yes";

             

             

            }

            else

            {

            this.getField("CheckBox1").value = "No";

              

            this.getField("CheckBox2").value = "No";

               

            }

            • 3. Re: Checking all checkboxes
              try67 MVP & Adobe Community Professional

              There's no point guessing at values. You need to check the properties of the fields and see what their export values are. The value when they're not selected is always "Off". That can't be changed.

              • 4. Re: Checking all checkboxes
                rxraza01 Level 1

                The Export Value is 'Yes' So I thought the following code will work but it is not. B/w I am checking that in Preview mode. Is there anything syntactically that I am doing wrong?

                 

                if (event.target.value !== "Off") {

                  this.getField("CheckBox1").value = "Yes";

                 

                  this.getField("CheckBox2").value = "Yes";

                 

                 

                }

                else

                {

                this.getField("CheckBox1").value = "Off";

                  

                this.getField("CheckBox2").value = "Off";

                   

                }

                • 5. Re: Checking all checkboxes
                  George_Johnson MVP & Adobe Community Professional

                  It should work if the name of the fields are exactly "CheckBox1" and "CheckBox2" and there are no leading or trailing spaces in the export values, such as "Yes " or " Yes ". So check the JavaScript console by pressing Ctrl+J to see if any errors are reported.

                  • 6. Re: Checking all checkboxes
                    rxraza01 Level 1

                    4:Field:Mouse Up

                    TypeError: getField("CheckBox1") is null

                    13:Field:Mouse Up

                    • 7. Re: Checking all checkboxes
                      rxraza01 Level 1

                      Thanks for pointing to Javascript Debugger. It really helped. The name had a space in between "Check Box1" instead of CheckBox1

                      • 8. Re: Checking all checkboxes
                        try67 MVP & Adobe Community Professional

                        Do not test the code in Preview mode. You need to exit the Form Edit more entirely to test it properly.

                        • 9. Re: Checking all checkboxes
                          gkaiseril MVP & Adobe Community Professional

                          You can always add some additional code to show what is going on with your script. The following code will catch and report field name errors and what the initial value is of all fields being processed:

                           

                          function GetField(cName) {
                          var oField = this.getField(cName);
                          if(oField == null) app.alert("Error accessing field named "+ cName, 0, 0);
                          return oField;
                          }

                          var cb1 = GetField("CheckBox1");
                          var cb2 = GetField("CheckBox2");
                          // soem debugging code;
                          console.show();
                          console.clear();
                          console.println("Target value: " + event.target.value);
                          if(cb1 != null) console.println("Check Box1 initial value: " + cb1.value);
                          if(cb2 != null) console.println("Check Box2 initial value: " + cb2.value);
                          // end debugging code;
                          if (event.target.value != "Off") {
                            cb1.value = "Yes";
                            cb2.value = "Yes";
                          } else {
                             cb1.value = "Off";
                             cb2.value = "Off";
                          }

                          The code works in preview.

                           

                          The function at the top can be placed as a document level function that can be used anytime a field is being accessed since it will report field name errors during development, during updating, or whenever the form is used.

                          • 10. Re: Checking all checkboxes
                            try67 MVP & Adobe Community Professional

                            Just a general note: I recommend using the checkThisBox method instead of applying the value directly.

                            This frees you up from hard-coding the export values into your code and makes it easier to control when the check-boxes are ticked or not.