3 Replies Latest reply on Mar 29, 2013 12:33 PM by resnickstark

    Java Script if then statement

    resnickstark

      I am creating a form in Acrobat Pro where one page has a drop down field name "State". In the State drop down it lists all 50 states full names. On another page in the same form the user would like the previous choice of "State" to carry down, but only have the Abbreviations of the States. I am really new to Java Script, but what I attempted (shown below) only carries down the full State name. Anyone know how to fix this?

       

      In this example I was trying to have the user Select Texas from above and in this field have TX show up. Instead Texas is entered automatically.

      (function () {

           var I9 = this.getField("State").value;

      if( State = "Texas") event.value = "TX";

      })

       

      Thanks in advance for your help.

      Christy

        • 1. Re: Java Script if then statement
          Test Screen Name Most Valuable Participant

          In JavaScript, you compare values using ==, not =. This is one of the classic errors, and some of us still make it regularly after decades of practice.

           

          A single = means something specific, it isn't an error. It just doesn't mean what you want.

          • 2. Re: Java Script if then statement
            George_Johnson MVP & Adobe Community Professional

            A simple way to do this is set the export value of each of the dropdown items to the abbreviation you want to use. The code would then simply be:

             

            // Calculate script

            event.value = getField("State").valueAsString;

             

             

            An alternative to all of the if statements is to set up an object with the state fullname/abbreviation pairs and look up the abbreviation:

             

             

            oStates = {"Alaska": "AK", "Washington": "WA", "Oregon": "OR"}  // Add other states as well

             

            event.value = oStates[getField("State").valueAsString] || "";

            • 3. Re: Java Script if then statement
              resnickstark Level 1

              George,

               

              Your post was very helpful! I used your idea of of setting up an object with the State full name/abbreviation that was great!

               

              I also figured out that I had placed my code in the Validation field instead of the calculation field so not much was changing.

               

              Thanks for your help!

              Christy