21 Replies Latest reply on Apr 21, 2008 11:57 AM by (kcdoell)

    A select all checkbox button

    Level 1
      Hello:

      I am a Newbie at JavaScript so please be patient and kind. I am currently creating a form using Adobe LiveCycle Designer 8.0.

      On my form I have a section that displays 50 check boxes representing 50 states in the USA. On my form it is reasonable that a end user could select some or all of the states. To that end, I want to have a checkbox or button that will select and deselect all of the State checkboxes. So far I have added a button called "Select All" and on the click event I wrote the following script:

      xfa.form.topmostSubform.Page2.CKSS_AL.rawValue=1
      xfa.form.topmostSubform.Page2.CKSS_AK.rawValue=1
      xfa.form.topmostSubform.Page2.CKSS_AR.rawValue=1
      xfa.form.topmostSubform.Page2.CKSS_AZ.rawValue=1
      xfa.form.topmostSubform.Page2.CKSS_CA.rawValue=1

      etc.....

      As you can see I have named my checkboxes CKSS_AL, CKSS_AK,,,etc..CKSS_WY.

      This works, but I am thinking that there must be a more efficient way of writing this code.... Is there a way to create a loop script that will accomplish the same thing? I have done this in VBA but never in JavaScript. I have been looking on the internet for ideas for days but keep coming up empty handed. If anyone can shed some light on the subject it would be greatly appreciated.

      Thanks,

      Keith.
        • 1. Re: A select all checkbox button
          Patrick Leckey Level 3
          Say you have an array of all your state acronyms (NY, AZ, CA, etc...) called 'stateArray' you can do as follows:

          for ( var state in stateArray )
          {
          var oField = xfa.resolveNode("xfa.form.topmostSubform.Page2.CKSS_" + state);
          oField.rawValue = 1;
          }

          Or, without your state array, you can do:

          var oFields = xfa.resolveNodes("xfa.topmostSubform.Page2.CKSS_*");
          for ( var oField in oFields )
          {
          oField.rawValue = 1;
          }

          Note that there is a difference between xfa.resolveNode and xfa.resolveNodes (with an 's').
          • 2. Re: A select all checkbox button
            Level 1
            Thanks for the quick reply. I don't have a state array or know how I would create one in a Adobe form so I copied your second script and pasted it in the click event of my SelectAll button:

            var oFields = xfa.resolveNodes("xfa.topmostSubform.Page2.CKSS_*");
            for ( var oField in oFields )
            {
            oField.rawValue = 1;

            But when I went to preview mode to see if it would work, clicking the button resulted in nothing happening.... Am I missing something?

            Thanks for your help.

            Keith.
            • 3. Re: A select all checkbox button
              Patrick Leckey Level 3
              Well, if exactly what you have above is what you copied, then you're missing a closing bracket '}' after 'oField.rawValue = 1;'.

              Past that it would be hard for me to troubleshoot without actually seeing the layout of your form.

              You could see if there are any errors in the JavaScript console. To open the console on Windows, press CTRL+J. On Mac, (Freeway Interchange Symbol)+J. Copy any error messages after the line that reads 'Acrobat SOAP 8.0' and post them here.
              • 4. Re: A select all checkbox button
                Patrick Leckey Level 3
                Actually, I apologize, the 2nd method won't work with the way you have your form setup - it's an invalid SOM expression we're passing to resolveNodes. XFA forms are very particular about their field arrays.

                I created a form with 3 checkboxes and a button. The checkboxes were named CB_AZ, CB_NY, CB_CA (I didn't feel like doing all 50 states and being Canadian I'm not sure I would know all 50 acronyms).

                I then added this code to the 'click' event of the button, and it successfully selected all 3 checkboxes:

                var stateArray = new Array("AZ", "NY", "CA");
                for ( var state in stateArray )
                {
                var oField = xfa.resolveNode("xfa.form.form1.page1.CB_" + stateArray[state]);
                oField.rawValue = 1;
                }
                • 5. Re: A select all checkbox button
                  Level 1
                  Hello:

                  I forgot to copy over the closing bracket on my last reply. With that said I took your Array and modified it with the following:

                  var stateArray = new Array("AL", "AK", "AR");
                  for ( var state in stateArray )
                  {
                  var oField = xfa.resolveNode("xfa.topmostSubform.Page2.CKSS_" + stateArray[state]);
                  oField.rawValue = 1;
                  }

                  I only did the array for the first 3 states/checkboxes. Then I copied it into the click event of my "SelectAll" button. I received the following error:

                  oField has no properties
                  5:XFA:topmostSubform[0]:Page2[0]:SelectAll[0]:click
                  oField has no properties
                  5:XFA:topmostSubform[0]:Page2[0]:SelectAll[0]:click

                  Any idea, I am thinking that now at least I am getting an error message so I must be close. Any ideas?

                  Thanks for your help,

                  keith.
                  • 6. Re: A select all checkbox button
                    Patrick Leckey Level 3
                    I wouldn't be able to give you more guidance without knowing your form layout. Could you post a screenshot of your form hierarchy?

                    Here is how my form looks and it works to check all 3 boxes:
                    http://img216.imageshack.us/my.php?image=checkboxjq9.jpg
                    • 7. Re: A select all checkbox button
                      Level 1
                      Hello below is the structure:

                      >topmostSubform
                      >Page1
                      >Page2
                      CKSS_AL
                      CKSS_AK
                      CKSS_AR
                      CKSS_CO
                      etc........
                      >Page3
                      • 8. Re: A select all checkbox button
                        Level 1
                        I have other controls textboxes and dropdown lists etc. under Page2 but the structure is basically laid out as above.

                        Thanks for any ideas you may have.

                        Keith.
                        • 9. Re: A select all checkbox button
                          Patrick Leckey Level 3
                          Are Page1, Page2 and Page3 under your (Master Pages) tree?

                          Also, try changing "xfa.topmostSubform" to "xfa.form.topmostSubform".
                          • 10. Re: A select all checkbox button
                            Level 1
                            Bingo, and Amen! It was missing the .form; below was the final solution:

                            var stateArray = new Array("AL", "AK", "AR");
                            for ( var state in stateArray )
                            {
                            var oField = xfa.resolveNode("xfa.form.topmostSubform.Page2.CKSS_" + stateArray[state]);
                            oField.rawValue = 1;
                            }

                            You dont know how you have made my day. I have been working on this one for too long with no results.

                            Thanks and have a great night!

                            Best regards,

                            Keith.
                            • 11. Re: A select all checkbox button
                              Level 1
                              PDL:

                              I don't know if you are online but I have another question that I have been trying to figure out. I applied the code in the click event to a check box called "AllState". Is there a way that when I uncheck the box it will clear out all the states?

                              If there is anybody else that has an idea, I would greatly appreciate any ideas.

                              Thanks,

                              Keith.

                              P.S. Below is my code:

                              var stateArray = new Array("AL", "AK", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY");
                              for ( var state in stateArray )
                              {
                              var oField = xfa.resolveNode("xfa.form.PROPAC_APP.PROPAC_Page2.Subform2.tblStatesUSA.Row[*].CSKK_" + stateArray[state]);
                              oField.rawValue = 1;
                              }
                              • 12. Re: A select all checkbox button
                                Patrick Leckey Level 3
                                Just check for it's own checked-state (this.rawValue). If it's own state is checked (1) then do your loop as above. If it's unchecked (0) then do the same loop as above but set oField.rawValue = 0.

                                This can all be done from the same click event. Define your stateArray outside of the if-check so that it only needs to be defined once (that way when Quebec separates from Canada and joins the US you only need to add the new state acronym once).

                                -- click event of AllState checkbox --
                                var stateArray = new Array (...)
                                if ( this.rawValue == 1 )
                                {
                                <your above code, setting oField.rawValue to 1>
                                } else {
                                <your above code, only set oField.rawValue to 0>
                                }
                                • 13. Re: A select all checkbox button
                                  Level 1
                                  PDL:

                                  Perfect, thanks a million!

                                  Keith.
                                  • 14. Re: A select all checkbox button
                                    Level 1
                                    PDL:

                                    Is there an easy way to put an additional code where if any of the state check boxes were unchecked (after a user clicked on my "CheckAllStates" check box, thus all The "state" check boxes are checked), that the check box I call "CheckAllStates" would uncheck??

                                    Currently this action keeps my "CheckAllStates" marked as checked. My code is as follows in the click event of my "checkAllStates" check box:

                                    PROPAC_APP.PROPAC_Page2.Subform2.tblStatesUSA.Row[4].CheckALLStates::click - (JavaScript, client)
                                    var stateArray = new Array("AL", "AK", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY");
                                    for ( var state in stateArray )
                                    if (this.rawValue==1)
                                    {
                                    var oField = xfa.resolveNode("xfa.form.PROPAC_APP.PROPAC_Page2.Subform2.tblStatesUSA.Row[*].CSKK_" + stateArray[state]);
                                    oField.rawValue = 1;
                                    } else {
                                    var oField = xfa.resolveNode("xfa.form.PROPAC_APP.PROPAC_Page2.Subform2.tblStatesUSA.Row[*].CSKK_" + stateArray[state]);
                                    oField.rawValue = 0;
                                    }

                                    Any help would be great.

                                    Thanks,

                                    Keith.
                                    • 15. Re: A select all checkbox button
                                      Level 1
                                      PDL:

                                      Not too sure if you will get this reply since it has been awhile and I thought I was done....

                                      Hopefully you will get it.

                                      Hope all is well,

                                      Keith.
                                      • 16. Re: A select all checkbox button
                                        Patrick Leckey Level 3
                                        In the "change" event of each states own checkbox, add the following:

                                        if ( this.rawValue == 0 ){
                                        var oField = xfa.resolveNode("PROPAC_APP.PROPAC_Page2.Subform2.tblStatesUSA.Row[4].CheckALLStates");
                                        oField.rawValue = 0;
                                        }
                                        • 17. Re: A select all checkbox button
                                          Level 1
                                          PDL:

                                          That worked great, thanks. I just noticed that if I was to immediately check the box again (then all the states were checked) the CheckAllStates would not be checked. Is there a way to cover that angle as well?

                                          Best regards,

                                          Keith.
                                          • 18. Re: A select all checkbox button
                                            Patrick Leckey Level 3
                                            That would require a loop in each of your individual state checkboxes that cycles through all the states and checks to see if they are checked. Have a boolean value that is by default true, and gets set to false if the rawValue of any of the state check boxes are not checked. If it is still true at the end of the loop, set the rawValue of the checkAllStates box to 1.
                                            • 19. Re: A select all checkbox button
                                              Level 1
                                              PDL:

                                              Yes, I figured that somehow it would have to be some sort of loop code to check the status of the other check boxes. I am thinking that I would leave the change event as is and apply the loop to the onClick event. I believe a boolean is like a true false status, how would I assign the boolean value to a checkbox? I guess I would need to build back in my state array to cycle through the state check boxes as well? What would be the code verbiage for Javascript?

                                              Thanks again,

                                              Keith.
                                              • 20. Re: A select all checkbox button
                                                Patrick Leckey Level 3
                                                You don't assign a boolean value to the check box. You create a boolean variable inside the check box's script, like this:

                                                var bAllChecked = true;

                                                Then in your loop, if you find any unchecked boxes (boxes whose rawValue is 0), do this:

                                                bAllChecked = false;

                                                Then after your loop, if bAllChecked is still true then set the CheckAllBoxes rawValue to 1. If it is false, set CheckAllBoxes rawValue to 0.
                                                • 21. Re: A select all checkbox button
                                                  Level 1
                                                  PDL:

                                                  I will have to read up on that one. I never did that type of loop let alone in JavaScript.

                                                  Thanks for getting back,

                                                  Keith.