21 Replies Latest reply on Sep 5, 2013 5:55 PM by M.Su

    Lengthy Drop Down List Items

    M.Su Level 1

      I am completely new to Acrobat and Javascripts.

       

      Are there any scripts that can:

      - Wrap text within a drop down list?

      - Automatically wrap long drop down items into a text field below it?

       

      Or, if there is an invisible multiline text field beneath the drop down list, is there a script that can:

      - Make the text field visible and the drop down list invisible when the entry reaches the right margin,

      - Automatically move the drop down item or custom text into the text field,

      - Still allow the user to select a different item from the drop down list if necessary?

       

      Please kindly include directions as to how and where the scripts should be applied if any of these are possible.

       

      Thank you dearly in advance.

        • 1. Re: Lengthy Drop Down List Items
          MichaelN Level 3

          Here is a script to put the selected item from a combo box menu into a text field.

           

            

               //Place this script in the custom validation script of the drop down menu

               //Create a text field called myTextField

           

               var MySelection = event.value;

               switch (MySelection) {

               case "Item1 Description":

               this.getField("myTextField").value = "Item1 Description";

              

               break;

              

               case "Item2 Description":

               this.getField("myTextField").value = "Item2 Description";

              

               break;

           

               //etc.

           

               }

           

           

          I hope this helps.

          • 2. Re: Lengthy Drop Down List Items
            M.Su Level 1

            Thank you. I've tried using the script, pasting it exactly as it is into the custom validation script of the drop down menu after creating "myTextField".

             

            It didn't quite work. Am I supposed to replace the "Item1 Description"? If so, what should it be replaced with?

             

            Also, my drop down menus have over twenty items each... do I need to list each of them in the script?

             

            Is there any way to modify the script so that it is not necessary to do so and to hide the drop down menu so that only the text field is visible after a selection is made?

             

            As well, would this script work if the user types in a custom entry instead of selecting one of the items in the drop down?

             

             

            Thank you for all your help!

            • 3. Re: Lengthy Drop Down List Items
              MichaelN Level 3

              M.Su wrote:

               

              Thank you. I've tried using the script, pasting it exactly as it is into the custom validation script of the drop down menu after creating "myTextField".

               

              It didn't quite work. Am I supposed to replace the "Item1 Description"? If so, what should it be replaced with?


              You replace it with the first item from your menu.

               

              Also, my drop down menus have over twenty items each... do I need to list each of them in the script?

              Yes

               

              Is there any way to modify the script so that it is not necessary to do so and to hide the drop down menu so that only the text field is visible after a selection is made?
               
              But if the menu is hidden, how will the user be able to choose another item?

               

              As well, would this script work if the user types in a custom entry instead of selecting one of the items in the drop down?

              Yes

               

               

              Thank you for all your help!


              Also, make sure that you have selected "Commit selected value immediately" chosen (Combo Box Properties > Options tab)

              • 4. Re: Lengthy Drop Down List Items
                MarkWalsh Level 4

                Just use this in the combobox's validate event:

                 

                this.getField("myTextField").value = event.value;

                 

                Be sure to substitute the actual name of your text field.

                 

                 

                As for the setting the text field to be visible, for something like this, I usually just set the ComboBox to 'Visible but doesn't print' and the text field to 'Hidden but printable'.

                • 5. Re: Lengthy Drop Down List Items
                  MichaelN Level 3

                  Thanks Mark.

                   

                  Your 1 line script is a simple and elegant solution, and puts my feeble attempt to shame.

                   

                  I'm still learning scripting and the experts on this forum are great.

                  • 6. Re: Lengthy Drop Down List Items
                    M.Su Level 1

                    Sorry about the late reply, but thank you so much! I really appreciate your and MichaelN's help.

                    • 7. Re: Lengthy Drop Down List Items
                      M.Su Level 1

                      Is it possible to set it so that a text field can display the selections of three comboboxes?

                       

                      For example, if the three comboboxes are 'A', 'B', and 'C', what the users select will be displayed as below in the text field:

                       

                      A

                      B

                      C

                       

                      Can it also take into account that one perhaps two of the comboboxes may be left empty and that the default text should not be shown in the text field?

                      • 8. Re: Lengthy Drop Down List Items
                        MichaelN Level 3

                        Set this as the custom calculation script for the text field:

                         

                             event.value = getField("A").valueAsString + "\n" + getField("B").valueAsString + "\n" + getField("C").valueAsString;

                         

                        Make sure the text field is set to Multi-line.

                         

                        I'm not sure about your second question.

                         

                        I hope this helps.

                        • 9. Re: Lengthy Drop Down List Items
                          M.Su Level 1

                          Thanks a lot. Do you know if it can commit the value immediately? Right now, the text field needs to be reset to reflect any changes in the combobox values. (Select all text < Delete < Deselect field < New changes shown)

                          • 10. Re: Lengthy Drop Down List Items
                            MichaelN Level 3

                            Yes, the combo boxes A, B and C need to be set to "Commit selected value immediately". When I tested it, the script worked OK.

                            • 11. Re: Lengthy Drop Down List Items
                              M.Su Level 1

                              Is it possible to add an 'if statement' along the lines of "If value of Combobox A / Combobox B / Combobox C is not null or (the default text)"  before the value is entered in the text field? And do you know of any way to set a center line alignment?

                              • 12. Re: Lengthy Drop Down List Items
                                MichaelN Level 3

                                This may not be the best way to do it, but here goes...

                                 

                                Give the 3 combo boxes a default value of "Please Select..."

                                 

                                The blank form will look something like this:

                                 

                                form1.jpg

                                 

                                Then try this script as the calculation script of the text field:

                                 

                                     var a = getField("A").value
                                     var b = getField("B").value;
                                     var c = getField("C").value;

                                     if ((a && b && c)!= "Please Select..."){
                                     event.value = a + "\n" + b + "\n" + c;
                                     }
                                     else event.value = "";

                                 

                                This will give you the following result after ALL 3 choices have been made:

                                 

                                form2.jpg

                                 

                                If you use || (the OR operator) in line 4:

                                 

                                     if ((a || b || c)!= "Please Select..."){

                                 

                                the result will be:

                                 

                                form3.jpg

                                 

                                To centre the text, just change the alignement to "Center" in Text Field Properties.

                                 

                                I hope this helps.

                                • 13. Re: Lengthy Drop Down List Items
                                  M.Su Level 1

                                  Thank you! Can you please tell me if I am doing something incorrect here? I am trying to set it so that:

                                  - If 'c' is left as default, only 'a' & 'b' will show.

                                  - If 'a', 'b', & 'c' are left as default, nothing will show.

                                   

                                  var a = getField("A").value

                                  var b = getField("B").value;

                                  var c = getField("C").value;

                                   

                                   

                                  if (c = "Add a comment") {event.value = a + "\n" + b;}

                                  if ((a && b && c) == "Add a comment")

                                  {event.value = "";}

                                  • 14. Re: Lengthy Drop Down List Items
                                    MarkWalsh Level 4

                                    Try this (untested). NOTE that in the second 2 cases, either A and/or B could be the default. If your goal is to only show non-default values, then that would be done a different way.

                                     

                                    // Get current Values
                                    var a = getField("A").value;
                                    var b = getField("B").value;
                                    var c = getField("C").value;

                                     

                                    // Get default Values
                                    var aD = getField("A").defaultValue;
                                    var bD = getField("B").defaultValue;
                                    var cD = getField("C").defaultValue;

                                     

                                    switch(true) {
                                    case (a == aD) && (b == bD) && (c == cD):
                                    // All are default
                                    event.value = "";
                                    break;
                                    case (c == cD):
                                    // C is default - NOTE - A or B could be default
                                    event.value = a + "\n" + b;

                                    break;
                                    default:
                                    // C is NOT default - NOTE - A and/or B could be default
                                    event.value = a + "\n" + b + "\n" + c;
                                    }

                                    • 15. Re: Lengthy Drop Down List Items
                                      M.Su Level 1

                                      It's working perfectly. Hopefully this will be the last question.

                                       

                                      I've used a spawning template to create 40 new pages, renaming all the fields. The script remains the same, but since all the field names have changed, I figured all I would have to do is change the field names in the script to coincide.

                                       

                                      What I've essentially changed is...

                                       

                                      // Get current Values
                                      var a = getField("P1.myTemplate.A").value;
                                      var b = getField("P1.myTemplate.B").value;
                                      var c = getField("P1.myTemplate.C").value;

                                       

                                      // Get default Values
                                      var aD = getField("P1.myTemplate.A").defaultValue;
                                      var bD = getField("P1.myTemplate.B").defaultValue;
                                      var cD = getField("P1.myTemplate.C").defaultValue;

                                       

                                      ...leaving the rest of the script intact.

                                       

                                      The first initial page is now working, but the second page (P1) is not. Do you know what I am doing wrong?

                                      • 16. Re: Lengthy Drop Down List Items
                                        MarkWalsh Level 4

                                        Sorry, it's been a long time since I had to deal with templates (and I'm not even sure if Iused calculated fields when I did), so I'm a bit rusty on their usage.

                                        • 17. Re: Lengthy Drop Down List Items
                                          gkaiseril MVP & Adobe Community Professional

                                          This is a tricky process. Note all JavaScripts  accessing any field will need to be a custom script.

                                           

                                          When I create a form that uses templates, I build the template and keep it visible. From that template I spawn the first page before the visible template and I add the basic calculations. I then spawn the first form page before the template. I now have a form page with the form names including the "p#" prefix. I now adjust the calculations to include the p# and template name levels as variables concatenated to the field names as needed.. You may also need to adjust the scripts to allow for identification of the template or a spawned form page. Once all of the scripts for the "p0" page have been debugged, I add the scripts to the template and hide the template. Then I test spawning new pages to the end of the PDF. In this manner, the "#" for the "p#' level will match the value for "this.pageNum".

                                           

                                          Note the first page must be a form page spawned from the template. You can use the "event.target.name" to get the name of the field the custom script is associated with. Using the "event.target.name.split(".")" will create an array of the elements for the hierarchical field name and the 0 element will be "p#" value for the field and the 1 element will be template name. From there one can compute any renamed field on the spawned template. One could also use this test if a field is on a spawned page or the template.

                                           

                                          A Lesson in Templates for Adobe Acrobat by Dave Wraight is old but it still applies to AcroForms.

                                          • 18. Re: Lengthy Drop Down List Items
                                            gkaiseril MVP & Adobe Community Professional

                                            For skipping the empty or default values form a list I would look the the document level script "fillin" for the sample form "PFH.PDF" supplied with the distribution CD/DVD for Acrobat.

                                             

                                            // Concatenate 3 strings with separators where needed
                                            function fillin(s1, s2, s3, sep) {
                                              var test = 0;

                                              s1 = s1.toString();
                                              s2 = s2.toString();
                                              s3 = s3.toString();

                                              if (s1 != "") test += 4;
                                              if (s2 != "") test += 2;
                                              if (s3 != "") test += 1;

                                              if (test == 0) return "";
                                              if (test == 1) return s3;
                                              if (test == 2) return s2;
                                              if (test == 3) return s2 + sep + s3;
                                              if (test == 4) return s1;
                                              if (test == 5) return s1 + sep + s3;
                                              if (test == 6) return s1 + sep + s2;
                                              if (test == 7) return s1 + sep + s2 + sep + s3;
                                            } // end fillin function

                                             

                                            // get field objects:
                                            var aF = this.getField("Combo Box1")
                                            var bF = this.getField("Combo Box2")
                                            var cF = this.getField("Combo Box3")

                                             

                                            // set values that are not default values
                                            var a = aF.value == aF.defaultValue? "" : aF.value;
                                            var b = bF.value == bF.defaultValue? "" : bF.value;
                                            var c = cF.value == cF.defaultValue? "" : cF.value;

                                             

                                            // report the selections:
                                            event.value = fillin(a, b, c, "\n");

                                            • 19. Re: Lengthy Drop Down List Items
                                              M.Su Level 1

                                              Thanks, but I think this is a little beyond my understanding. Basically though, is it that the calculation scripts will not work within the spawned pages even if I adjust the form names?

                                              • 20. Re: Lengthy Drop Down List Items
                                                gkaiseril MVP & Adobe Community Professional

                                                There is no reason they should not. You only need to adjust the code that gets the field objects.

                                                 

                                                var cPrefix = this.pageNum + "." + "MyTemplate.";

                                                // get field objects:
                                                var aF = this.getField(cPrefix +  "Combo Box1");
                                                var bF = this.getField(cPrefix +  "Combo Box2");
                                                var cF = this.getField(cPrefix +  "Combo Box3");

                                                • 21. Re: Lengthy Drop Down List Items
                                                  M.Su Level 1

                                                  Is there anything wrong with this? When I applied it, the second page didn't work.

                                                   

                                                   

                                                  P0 Calculation Script

                                                   

                                                  // Get Current Value

                                                  var a = getField("Oral Communication Comment #1").value;

                                                  var b = getField("Oral Communication Comment #2").value;

                                                  var c = getField("Oral Communication Comment #3").value;

                                                   

                                                  // Get Default Values

                                                  var aD = getField("Oral Communication Comment #1").defaultValue;

                                                  var bD = getField("Oral Communication Comment #2").defaultValue;

                                                  var cD = getField("Oral Communication Comment #3").defaultValue;

                                                   

                                                  switch(true) {

                                                  case (a == aD) && (b == bD) && (c == cD):

                                                  event.value = "";

                                                  break;

                                                  case (b == aD) && (c == cD):

                                                  event.value = a;

                                                  break;

                                                  case (c == cD):

                                                  event.value = a + "\n" + b;

                                                  break;

                                                  default:

                                                  event.value = a + "\n" + b + "\n" + c;

                                                  }

                                                   

                                                   

                                                   

                                                  P1 Calculation Script

                                                   

                                                  var cPrefix = this.pageNum + "." + "myTemplate.";

                                                  // get field objects:

                                                  var a = this.getField(cPrefix + "Oral Communication Comment #1");

                                                  var b = this.getField(cPrefix + "Oral Communication Comment #2");

                                                  var c = this.getField(cPrefix + "Oral Communication Comment #3");

                                                   

                                                  // Get Default Values

                                                  var aD = getField(cPrefix + "Oral Communication Comment #1").defaultValue;

                                                  var bD = getField(cPrefix + "Oral Communication Comment #2").defaultValue;

                                                  var cD = getField(cPrefix + "Oral Communication Comment #3").defaultValue;

                                                   

                                                  switch(true) {

                                                  case (a == aD) && (b == bD) && (c == cD):

                                                  event.value = "";

                                                  break;

                                                  case (b == aD) && (c == cD):

                                                  event.value = a;

                                                  break;

                                                  case (c == cD):

                                                  event.value = a + "\n" + b;

                                                  break;

                                                  default:

                                                  event.value = a + "\n" + b + "\n" + c;

                                                  }

                                                   

                                                   

                                                  This doesn't work for the second page either:

                                                   

                                                  P1 Calculation Script

                                                   

                                                  // Get Current Value

                                                  var a = getField("P1.myTemplate.Oral Communication Comment #1").value;

                                                  var b = getField("P1.myTemplate.Oral Communication Comment #2").value;

                                                  var c = getField("P1.myTemplate.Oral Communication Comment #3").value;

                                                   

                                                  // Get Default Values

                                                  var aD = getField("P1.myTemplate.Oral Communication Comment #1").defaultValue;

                                                  var bD = getField("P1.myTemplate.Oral Communication Comment #2").defaultValue;

                                                  var cD = getField("P1.myTemplate.Oral Communication Comment #3").defaultValue;

                                                   

                                                  switch(true) {

                                                  case (a == aD) && (b == bD) && (c == cD):

                                                  event.value = "";

                                                  break;

                                                  case (b == aD) && (c == cD):

                                                  event.value = a;

                                                  break;

                                                  case (c == cD):

                                                  event.value = a + "\n" + b;

                                                  break;

                                                  default:

                                                  event.value = a + "\n" + b + "\n" + c;

                                                  }