9 Replies Latest reply on Nov 6, 2012 11:00 PM by lifeguard23

    Conditional Checkboxes in Acrobat (help with basic JavaScript)

    lifeguard23

      I will start by saying I have minimal knowledge of programming/scripting.  I am assuming this shouldn't be too difficult, but I can't get it working (I got somewhat close using the discussion at http://forums.adobe.com/thread/797188,  but it still isn't working.

       

      I have an acrobat document with 2 checkboxes (cb1 and cb2).  By default they should both be off.  If the user puts a check in cb1, I want a check to automatically show up in cb2.  If cb1 is then unchecked, then cb2 is unchecked.  I'm sure it's a simple code, but I'm still lost. 

       

      My basic understanding is that I need to create the code in a document-wide JavaScript.  Then run that code with a "Mouse Up" on cb1.  A step by step would be greatly appreciated.

        • 1. Re: Conditional Checkboxes in Acrobat (help with basic JavaScript)
          George_Johnson MVP & Adobe Community Professional

          If you want the two check boxes to always mirror each other, give each the same field name and same export value. No JavaScript is needed.

          • 2. Re: Conditional Checkboxes in Acrobat (help with basic JavaScript)
            lifeguard23 Level 1

            I didn't even think of that.  That totally would work.  However, I did over-simplify my request a little.  In retrospect, I probably should have actually laid out exactly what my purpose is.

             

            I have a test sheet for a certification course that I (along with about 200 other people in my region teach regularly).  The test sheet can hold 12 names on it (one of each student in the class).  For each name, there are 15 different items that they must complete in order to successfully pass the course.  For each item the candidate either gets a Checkmark (complete) or an "X" (incomplete).  All items must be checked off for all particpants (meaning 12 x 15 = 180 times that a checkmark or an X has to be inputted).

             

            The way the sheet is currently setup is that for each item that needs to be checked off, there is a checkbox (at the top of the box) and a text box (at the bottom).  (I've got an image kind of showing this at https://dl.dropbox.com/u/5892902/form.jpg).  Because each item is independent of one another, each checkbox has to have a different name.  But potentially, if I run a class of 12 students, and they all pass, I have to click 180 times to insert all 180 checkboxes....super annoying.

             

            I want to create a small, almost hidden, checkbox for each student that if I click it, it will automatically check off all of the checkboxes in that row. Basically a "this person passes so click this one button and all the necessary checkboxes will get filled). 

             

            So I guess I should have worded my original post more like the following:

            I want to check off cb1 and have cb2, cb3. cb4, cb5, cb6, cb7, cb8, etc. all automatically get checked.  This would save a lot of time of mass-clicking all the boxes

            • 3. Re: Conditional Checkboxes in Acrobat (help with basic JavaScript)
              George_Johnson MVP & Adobe Community Professional

              Got it. If I were doing this, I'd use hierarchical naming for the check boxes in the row. For example, the first row would have "cb.1.0" (the check box that toggles the other check boxes) and "cb.1.1" through "cb.1.15" for the first row of check boxes, "cb.2.0" through "cb.2.15" for the second row, etc. You could then create a document-level script that is called in the Mouse Up event of each "0" check box that could be something like:

               

              Mouse Up script for first check box in the row (cb.row_num.0)

              function checkAllInRow() {

               

                  // Determine the row number from the field name of the button that called this function

                  var row = event.target.name.split(".")[1];

               

                  // Get the value of this check box

                  var v = event.target.value;

               

                  // Set the new value check box state

                  var nv = v === "Off" ? false : true;

               

                  // Loop through the check boxes in the row and toggle their values

                  for (var col = 1; col < 16; col += 1) {

                      getField("cb." + row + "." + col).checkThisBox(0, nv);

                  }

               

              }

              • 4. Re: Conditional Checkboxes in Acrobat (help with basic JavaScript)
                George_Johnson MVP & Adobe Community Professional

                BTW, you can quickly make a grid of these check boxes by creating the first check box with a name of "cb" and the other properties set as you want, then right-click and select "Place Multiple Fields". Add one more row than you'll need and after the fields are created, delete the first row (e.g., "cb.0.0", "cb.0 1", etc.)

                • 5. Re: Conditional Checkboxes in Acrobat (help with basic JavaScript)
                  lifeguard23 Level 1

                  Wow!!  That works perfect.  Thank you so much.

                   

                  One other thing.  At the end of the 15 boxes, there is a final box in which we have an open text field that must read either "Pass" or "Fail."  Right now we are just manually typing in the result.  Is it possible to have that field check the entire row and if all checks are on, then it automatically inserts the text "Pass"?

                  • 6. Re: Conditional Checkboxes in Acrobat (help with basic JavaScript)
                    George_Johnson MVP & Adobe Community Professional

                    Sure. Is there a particular cutoff that determines whether it's pass or fail? For example, 10 or more correct is a Pass, otherwise Fail?

                    • 7. Re: Conditional Checkboxes in Acrobat (help with basic JavaScript)
                      lifeguard23 Level 1

                      All items must be complete otherwise it's a fail.  So 15/15 

                      • 8. Re: Conditional Checkboxes in Acrobat (help with basic JavaScript)
                        George_Johnson MVP & Adobe Community Professional

                        You could use a custom calculate script for the text field that checks each check box, something like:

                         

                        // Custom Calculate script for text field of each row

                        function calcPassFail() {

                         

                           // Determine the row number from the field name of the text field that called this function

                            var row = event.target.name.split(".")[1];

                         

                            // Loop through the check boxes in the row to see if any are unchecked (value is "Off")

                            for (var col = 1; col < 16; col += 1) {

                                if (getField("cb." + row + "." + col).value === "Off") {

                         

                                    // Blank this field, or optionally set the value to "Fail"

                                    event.value = "";  // or "Fail"

                                    return;

                         

                                }

                            }

                         

                            // All were selected, so indicate Pass

                            event.value = "Pass";

                         

                        }

                         

                         

                        Place this function in the document-level JavaScript and call it like this in the Calculate event of each row's text field:

                         

                        calcPassFail();

                         

                         

                        As is it blanks the field if they didn't pass, but you could change it to show "Fail" instead. This code assumes the names of the text boxes are like "grade.1", "grade.2", ..."grade.15" so that they are numbered like the check boxes.

                        • 9. Re: Conditional Checkboxes in Acrobat (help with basic JavaScript)
                          lifeguard23 Level 1

                          Works perfect.  You are a genius!  Thank you so much.  It is incredibly appreciated