12 Replies Latest reply on Nov 25, 2017 12:31 AM by dennisg19059968

    Select and Deselect All Checkboxes

    dennisg19059968

      Hi,

      I'm using Adobe acrobate dc.

       

      I have three checkboxes in my pdf-document.

       

      They are namned: checkbox1, checkbox2 and checkbox3

       

      I wish the following to happen:

       

      -When checkbox1 is selected, checkbox2 and checkbox3 also become selected.

      -When checkbox1 is deselected, checkbox2 and checkbox3 also become deselected.

       

      I'm more than a beginner to this with javascript, so please keep this in mind when

      explaining or giving me a working javascript.

       

      Thanks

      /Dennis

        • 1. Re: Select and Deselect All Checkboxes
          Thom Parker Adobe Community Professional

          By select, do you mean checked? Selected means the field has keyboard focus.

           

          Put your script on the MouseUp event for "checkbox1". All you need to do is set the value of the other checkboxes to the value of "checkbox1", which is "event.target.value.

           

          So the line of code for setting "checkbox2" is:

           

          this.getField("checkbox2").value = event.target.value;

           

           

           

          So what happens when the user changes the value of "checkbox2"? Does it affect other fields?

          • 2. Re: Select and Deselect All Checkboxes
            gkaiseril MVP & Adobe Community Professional

            The following script will check the other 2 check boxes if box 1 is checked and uncheck them when box 1 is unchecked and it does not even need to know the value of the checked boxes.

             

            // mouse up action JavaScript;

            // get field objects;

            var CB1 = this.getField("checkbox1");

            var CB2 = this.getField("checkbox2");

            var CB3 = this.getField("checkbox3");

            // check check box 1 value;

            if(CB1.value != "Off")

            {

            // check the ohter boxes;

            CB2.checkThisBox(0,true);

            CB3.checkThisBox(0,true);

            } else {

            // uncheck the other boxes;

            CB2.checkThisBox(0, false)

            CB3.checkThisBox(0, false);

            }

            • 3. Re: Select and Deselect All Checkboxes
              Thom Parker Adobe Community Professional

              Got a lot of code there, why so much?

               

              BTW: I corrected my previous post to use the correct event property for getting the value. It only takes two lines of code.  And it could be modified to use the "checkThisBox()" function if you wanted to avoid handling the export values.

              • 4. Re: Select and Deselect All Checkboxes
                dennisg19059968 Level 1

                Thank you Thom, the code

                 

                this.getField("checkbox2").value = event.target.value;

                 

                works fine and does its job.

                 

                As I'm a beginner of coding javascript and want to use this type of checkbox function on many locations and pages in my form, I would like to know what are the advantages and disadvantages of using export values instead of modifing it to use the "checkThisBox" instead?

                 

                Thanks a lot again!

                 

                /Dennis

                • 5. Re: Select and Deselect All Checkboxes
                  dennisg19059968 Level 1

                  Thank you gkaiseril for showing me how to do it with your code,

                   

                  /Dennis

                  • 6. Re: Select and Deselect All Checkboxes
                    Thom Parker Adobe Community Professional

                    For most applications it doesn't matter if the code checks the export value or the checked state. I find the export value to be more convenient. And in this case it results in tighter code, which is always a good goal to have for a script.

                     

                    However, testing for the checked state directly is more generic, if that is all you care about. One reason for doing this is for an automation script that needs to operate on checkboxes on any arbitrary form. In this situation you can't depend on the export value being the default "Yes".

                     

                    It is quite rare that there is an advantage to modifying the checkbox export value to something other than "Yes". But these situations do come up, and when they do the export value is critical. For example, Radio-Checkboxes.

                    1 person found this helpful
                    • 7. Re: Select and Deselect All Checkboxes
                      gkaiseril MVP & Adobe Community Professional

                      Sometimes commented and more obvious code might be more informative and maintainable.

                       

                      I can modify the 2 fields as described with just 2 lines of code.

                       

                      this.getField("checkbox2").checkThisBox(0, event.target.value != "Off");

                      this.getField("checkbox3").checkThisBox(0, event.target.value != "Off");

                       

                      And there is no dependency on the export value of the fields.

                      1 person found this helpful
                      • 8. Re: Select and Deselect All Checkboxes
                        Thom Parker Adobe Community Professional

                        I don't disagree, however, I think you can be too verbose. I think beginners do best with tighter, but not cryptic code. Simple and to the point. Cause I think too much structure is distracting, and can be confusing as well.

                        • 9. Re: Select and Deselect All Checkboxes
                          dennisg19059968 Level 1

                          Okay interesting, thank you Thom. Just for the discussion (I don't want this for now so no script is needed. I also know that what I'm going to describe below is way to complicated for a rookie like me at this stage, but it's interesting to know if future needs will call):

                           

                          Lets say that it's possible in a pdf-form that a log-in box appears every time you open this pdf-form. In the box you can choose to login in with different user names (e.g. userA, UserB etc).

                           

                          Now, after loging in with a username, if a checkbox (lets say namned checkboxA) is checked then this triggers a text to be generated in a text field on the last page of the form that says e.g.: "checkboxA was checked by user X at date XX-XX-XXXX".

                           

                          Would this type of script for checkboxA require that the code checks the checked state instead of the export value?

                          • 10. Re: Select and Deselect All Checkboxes
                            dennisg19059968 Level 1

                            Thanks gkaiseril for showing me another great way to do it.

                             

                            /Dennis

                            • 11. Re: Select and Deselect All Checkboxes
                              Thom Parker Adobe Community Professional

                              Given what you've described, the code would be almost exactly the same as the code we've provided above.

                              • 12. Re: Select and Deselect All Checkboxes
                                dennisg19059968 Level 1

                                Thanks Thom for your answers helping me understand some more about javascripts.

                                 

                                Have a nice weekend!

                                Dennis