10 Replies Latest reply on Aug 17, 2013 12:04 PM by luog1

    Javascript code for buttons, dropdowns, and text fields

    luog1

      Hello, I currently have a form with a dropdown field that fills a text field with something depending on what is selected on the dropdown via a custom keystroke script.

       

      I want to add a button that will input something into the text field depending on what is selected from the dropdown menu. Copying and pasting the keystroke script into the button does not work and returns an undefined value.

       

      I also want to add that I'm new to coding and it would be most helpful if someone can provide a script for this or let me know if its not possible. Thanks.

        • 1. Re: Javascript code for buttons, dropdowns, and text fields
          George_Johnson MVP & Adobe Community Professional

          Can you post the Keystroke code that you used?

          • 2. Re: Javascript code for buttons, dropdowns, and text fields
            luog1 Level 1

            (function () {

                if (event.willCommit) return;

                var aQuotes = [];

                aQuotes[0] = this.getField("Batch").value = util.printd("Rmmddyy-96", f) ;

                aQuotes[1] = this.getField("Batch").value = util.printd("Rmmddyy-95", f) ;

                aQuotes[2] = this.getField("Batch").value = util.printd("Rmmddyy-94", f) ;

                aQuotes[3] = this.getField("Batch").value = util.printd("Rmmddyy-93", f) ;

                aQuotes[4] = this.getField("Batch").value = util.printd("Rmmddyy-92", f) ;

                aQuotes[5] = this.getField("Batch").value = util.printd("Rmmddyy-91", f) ;

                aQuotes[6] = this.getField("Batch").value = util.printd("Rmmddyy-90", f) ;

                var item = event.changeEx

                getField("Batch").value = aQuotes[item];

            })();

            • 3. Re: Javascript code for buttons, dropdowns, and text fields
              George_Johnson MVP & Adobe Community Professional

              OK, the Mouse Up script for the button might look something like this:

               

              (function () {

               

                 var aQuotes = [];

                 aQuotes[0] = this.getField("Batch").value = util.printd("Rmmddyy-96", f) ;

                 aQuotes[1] = this.getField("Batch").value = util.printd("Rmmddyy-95", f) ;

                 aQuotes[2] = this.getField("Batch").value = util.printd("Rmmddyy-94", f) ;

                 aQuotes[3] = this.getField("Batch").value = util.printd("Rmmddyy-93", f) ;

                 aQuotes[4] = this.getField("Batch").value = util.printd("Rmmddyy-92", f) ;

                 aQuotes[5] = this.getField("Batch").value = util.printd("Rmmddyy-91", f) ;

                 aQuotes[6] = this.getField("Batch").value = util.printd("Rmmddyy-90", f) ;

               

                 // Get the (export) value of the selected item of the dropdown

                 var item = getField("dropdown1").value;

               

                 // Set the value of the text field

                 getField("Batch").value = aQuotes[item];

               

              })();

               

              Replace "dropdown1" in the code above with the actual name of the relevant dropdown field.

              • 4. Re: Javascript code for buttons, dropdowns, and text fields
                George_Johnson MVP & Adobe Community Professional

                BTW, I'm not sure what this type of code is intended to do:

                 

                aQuotes[0] = this.getField("Batch").value = util.printd("Rmmddyy-96", f) ;

                 

                 

                since I'd expect something along the lines of:

                 

                aQuotes[0] = util.printd("Rmmddyy-96", f) ;

                 

                but since the variable f isn't defined or set anywhere that I can see, it's not clear to me what it does exactly.

                • 5. Re: Javascript code for buttons, dropdowns, and text fields
                  luog1 Level 1

                  That worked perfectly, thanks a lot

                  • 6. Re: Javascript code for buttons, dropdowns, and text fields
                    luog1 Level 1

                    that is just something i added to the code. Originally it was just plain text, but I wanted the current date to be part of the field. Adding it seemed to work with no issues.

                    • 7. Re: Javascript code for buttons, dropdowns, and text fields
                      gkaiseril MVP & Adobe Community Professional

                      "undefined" usually means that something expected to be an object or variable has not been defined.

                       

                      Where is "f" defined?

                       

                      What does "f" represent?

                      Is it a number, text string, field, field value, etc?

                       

                      Using the anonymous function definition may optimize running the code but for debugging it hides a lot of valuable data.

                       

                      Do you know if your code is even executed?

                       

                      Have you tried adding code to print some information to the JavaScript console?

                       

                      console.show();console.clear(); // debug;
                      console.println("keystroke processing"); // debug;
                      function MyKeystroke () {
                          console.println("start code");
                      var aQuotes = [];
                          aQuotes[0] = this.getField("Batch").value = util.printd("Rmmddyy-96", f) ;
                          aQuotes[1] = this.getField("Batch").value = util.printd("Rmmddyy-95", f) ;
                          aQuotes[2] = this.getField("Batch").value = util.printd("Rmmddyy-94", f) ;
                          aQuotes[3] = this.getField("Batch").value = util.printd("Rmmddyy-93", f) ;
                          aQuotes[4] = this.getField("Batch").value = util.printd("Rmmddyy-92", f) ;
                          aQuotes[5] = this.getField("Batch").value = util.printd("Rmmddyy-91", f) ;
                          aQuotes[6] = this.getField("Batch").value = util.printd("Rmmddyy-90", f) ;
                          var item = event.changeEx
                          getField("Batch").value = aQuotes[item];
                      console.println("end code");
                      } // end function;

                      MyKeystroke(); // call funciton;

                       

                      Results with:

                       

                      keystroke processing
                      start code

                      TypeError: Invalid argument type.
                      Util.printd:6:AcroForm:Combo Box1:Keystroke
                      ===> Parameter oDate.5

                      1 person found this helpful
                      • 8. Re: Javascript code for buttons, dropdowns, and text fields
                        luog1 Level 1

                        Yes I was aware that f wasnt defined in the code, but for some reason selecting a value in the dropdown was still able to print the correct date with the corresponding 9X number.

                        • 9. Re: Javascript code for buttons, dropdowns, and text fields
                          gkaiseril MVP & Adobe Community Professional

                          Well it might not be correct. Any random answer could be right or wrong. Just because one gets a correct answer 10 times does not make the calculation correct it only shows the wrong input value has not been used.

                           

                          JavaScript assumes a value of the current date object when a date object is missing.

                           

                          What else is not working in your code?

                          1 person found this helpful
                          • 10. Re: Javascript code for buttons, dropdowns, and text fields
                            luog1 Level 1

                            everything in the code is working as intended as of now. i have since changed the way the script pulls the dates. the code for the button is now:

                             

                            cDate = this.getField("PREP").value;

                            var oDate = util.scand("m/d/yy", cDate);

                            oDate.setDate(oDate.getDate() + 1);

                            this.getField("PREP").value = util.printd("m/d/yy", oDate);

                            (function () {

                               var aQuotes = [];

                               aQuotes[0] = util.printd("Rmmddyy-96", oDate) ;

                               aQuotes[1] = util.printd("Rmmddyy-95", oDate) ;

                               aQuotes[2] = util.printd("Rmmddyy-94", oDate) ;

                               aQuotes[3] = util.printd("Rmmddyy-93", oDate) ;

                               aQuotes[4] = util.printd("Rmmddyy-92", oDate) ;

                               aQuotes[5] = util.printd("Rmmddyy-91", oDate) ;

                               aQuotes[6] = util.printd("Rmmddyy-90", oDate) ;

                               var item = getField("Dose").value;

                               getField("Batch").value = aQuotes[item];

                            })();

                             

                            There is an action on page open that fills the 'batch' and 'prep' fields with today's date. The custom keystroke script i originally had in the dropdown could not print advanced dates, but the button is able to do it.