Use data from one field to populate other fields in an acrobat fill-in forms

Update 10-16-2014; added a sample where two field values concatenates into one with ‘and’ as a separator.

I was just asked how to use data from one field to populate other fields in an acrobat fill-in form.
Turns out you just need to write a little javascript and add it to the action tab of the initial field in your form.

To begin let’s assume we’re using Acrobat Pro 8 or 9. If you use Live Cycle Designer for your form wizard, you’ll want to edit the form in Acrobat to follow these steps exactly.

For example,

  • Let’s start with two fields: Text1 and Text2
  • Edit Text1 and in the action tab choose the ‘On Blur’ trigger, ‘Run a JavaScript’ action and Add that action: image002
    • On Blur = ‘upon leaving the field’. So if some types in the field and tabs or clicks away from the field this action is triggered. image005
  • Next highlight and edit the Run a JavaScript action:

    • Create your script and add it*.

      • For this example, I want whatever I enter in Text1 to populate Text2 so I added this script: image008
        • Here’s the text:
          //Set the vars one and two:
                    var one = this.getField("Text1");
                    var two = this.getField("Text2");
          
                    //next check if two is blank and if so, populate it with one’s value
                    if(two.value==''||two.value==null){two.value=one.value}
                    
      • *Note: Only the ‘parent’ field needs this code. The fields that are being populated don’t need any javascript. You just need to know their names so you can reference them.
        This can be useful for capturing someone’s name and populating it into multiple places through a document. To add additional fields you would adapt the current code to accommodate the new fields. The benefit of checking for a blank first is that we ensure we’re not overwriting something the user has already put there.

    • Here’s an example of a script with 4 fields that will be populated by the content of our first field. Since we can use custom names for fields, the new fields will all be called Name# (Name1,Name2,…)
      • //set the vars
                var one = this.getField("Name1");
                var two = this.getField("Name2");
                var three = this.getField("Name3");
                var four = this.getField("Name4");
        
                //this function that checks if the destination field is blank and if so popluates it.
                function checkAndPopulate(orig,populate){
                if(populate.value==''||populate.value==null){populate.value=orig.value}
                }
        
                //copy value of one to two, three and four
                checkAndPopulate(one,two);
                checkAndPopulate(one,three);
                checkAndPopulate(one,four);
        • Here’s the visual aide: image004
    • Blank form:
    • I type in the first field and it populates the rest: image009
    • But if we start over with blank fields and I type something in Name 3 it will not be overwritten when typing in the original name field: image006

    There you have it.

    Here’s the test document I created to try it out: fill-in-javascript_test


    Here’s a link to a new PDF sample where two field values concatenates into one with ‘and’ as a separator. Uploaded 10-16-2014:
    concat_javascript_test

    Here’s the code:

    //set the vars
    var one = this.getField("Name1");
    var two = this.getField("Name2");
    var three = this.getField("Name3");
    
    //this function concatenates two field values into one with 'and' as a separator.
    function concatFields(field1,field2,fieldDest){
     //ensure all vars are present
     if(field1 && field2 && fieldDest){
    		var fieldVals = new Array();
      if(field1.value!='' && field1.value!=null){
       fieldVals.push(field1.value);
      }
      if(field2.value!='' && field2.value!=null){
       fieldVals.push(field2.value);
      }
      fieldDestResult = fieldVals.join(' and ');
      //only populate the fieldDest if it's empty
      //if(fieldDestResult.value!=''||fieldDestResult.value!=null){
       fieldDest.value=fieldDestResult;
      //}
     }
    }
    concatFields(one,two,three);
  • Comments
    • Vid says:

      This is a simple example, if you really only want to have the value of one field repeat across multiple fields then you can name each relevant field the same name as the parent field.
      In LiveCycle There’s one additional step: Each of these field also needs to have their Object Binding set to ‘global’; Window > Object -> Click on the field or fields -> Select the ‘Binding’ tab in the Object window and change the default binding to Global.

      • AK says:

        Hi, I tried posting a couple items and not getting any reply.
        Hoping you can help.
        I used your script example to combine fields on an Adobe Form.
        I also included the text “and” in between the two fields, however, when the second field is null, I’d like the “and” omitted.
        Any thought? Script?
        Thank you!

    • Darren Smith says:

      Thankyou SO VERY MUCH; EXACTLY what I wanted (I even understood the logic of the code). Thankyou!

    • Darren Smith says:

      Thankyou SO VERY MUCH; EXACTLY what I wanted (I even understood the logic of the code). Thankyou!

    • Ajay says:

      In my case, i have a field “Working Month(mmmm-yyyyy)” and below in a grid a set of eight dates (mmm-yy). By default the working month is set to (month-1) ie last month. But if somebody changes this to say “May/2013” i want it to reflect in all other eight fields. Problem is date & format. On top it is mmmm-yyyy and below it is mmm-yy. Any help is appreciated!

    • Garth Wight says:

      Hi Vid,
      I’m new to Acrobat and forms. I am trying to build a form that calculates weights for an aircraft load sheet! Is there a JS that can compare the weights in three fields and populate the forth field with the lowest (lightest) weight?
      Acrobat XI Apple Mac

    • Glen says:

      It works very well, but I have a question. Is there a way that the field can dynamically fill in the value of a drop down, where someone might change the selection. Right now when I set it up once it enters the field value once, and the selection is changed, it stays as the original selection.

      Just curious you have a solution for that.

      Thanks,

      Glen

      • vid@uoregon.edu says:

        Hmm, it sounds plausible, but I haven’t tried anything like that with drop down’s. The key may be differentiating between the value of the field vs. what’s selected. Value may not change until the form is saved but selected would reflect the live change.

      • Bryn says:

        I had the same problem with a recent form. I use this script and it works well for me:

        //Set the vars one and two:
        var one = this.getField(“Text1”);
        var two = this.getField(“Text2″);
        //clear any text from two:
        if(two.value!=”||two.value!=null){two.value=”}
        //next check if two is blank and if so, populate it with one’s value
        if(two.value==”||two.value==null){two.value=one.value}

        • Murray says:

          Hi, I get an error on line 3.

          //Set the vars one and two:
          var one = this.getField(“Text1″);
          var two = this.getField(“Text2″);
          //clear any text from two:
          if(two.value!=”||two.value!=null){two.value=”}
          //next check if two is blank and if so, populate it with one’s value
          if(two.value==”||two.value==null){two.value=one.value}

          • vid@uoregon.edu says:

            I looked at the code snippet and I see a mix of curly and simple quotes. They need to match and simple quotes are ideal. If you can’t tell which is which with your current font, just re-type all the quotes you’ll be in good shape.

            • Murray says:

              Thanks for the answer. Tried and did not work. I changed the ” for ‘ and nothing happens.

              //Set the vars one and two:
              var one = this.getField(‘Text1’);
              var two = this.getField(‘Text2’);
              //clear any text from two:
              if(two.value!=”||two.value!=null){two.value=”}
              //next check if two is blank and if so, populate it with one’s value
              if(two.value==”||two.value==null){two.value=one.value}

              • vid@uoregon.edu says:

                Makes sense, there are other un-matched quote further down in the code. If I address all the quotes it looks like this:
                //Set the vars one and two:
                var one = this.getField(“Text1”);
                var two = this.getField(“Text2″);
                //clear any text from two:
                if(two.value!=””||two.value!=null){two.value=””}
                //next check if two is blank and if so, populate it with one’s value
                if(two.value==””||two.value==null){two.value=one.value}

    • Nani says:

      Thank you for your clear instructions, this was exactly what I was looking for! Even though I have no JS experience, because of the clarity of your explanation, I was able to edit the JS to overwrite the second field if something was already in there. On my form, if I later went back to correct the first field (Name), it would not correct the second field. That was fun!

      Also thanks for adding the extra tip for multiple fields.

    • mn49 says:

      help! i need to have adobe autopopulate a field based on a dropdown menu choice (ex. dropdown community name X then autopopulates community # for X as 12345).

      i am totally unfamiliar with js – how do i do this??
      thanks!

    • AK says:

      Hi, I used this script to combined fields with names in each and also included the word “and” in between. However, if there is only one name (only one of fields with data), how do I omit the “and” after the first and only name?

    • AK says:

      Please help.
      Used this script to combine 2 fields (Seller1, Seller2). Also added the word “and” in between, however, when on one Seller (Seller1) I need the word “and” omitted.
      I can’t find any script to accomplish this.
      I am not familiar with script.
      Would any one please provide the script to add this item?
      Thank you!

    • Alexis says:

      Okay, I utilized the script and it works great but I am having two problems:

      1) It will not auto populate the other fields unless after I type into origin field I click on it again then the others populate.

      2) After I get it to populate if I want to change the information and put something else there instead, my orgin changes of course but my fields that are set to auto populate will not without me clearing the form and starting over.

      • ASut says:

        Having exactly the same issue as identified in item #1. Not having any luck locating a resolution on the internet and, considering it’s been over a year since you posted the issue with no response, that I’m going to have to keep looking.

      • Babs Devan says:

        solution is simple, instead of using “On Blur – Run a JavaScript” use “Mouse Up – Run a JavaScript” (under actions).

    • Matt says:

      I am looking for a script for a button that will populate a set of ID #’s. I have a form that has 25 rows and each has a sequentially number ID # and I would like to have the button add 25 to each field so I can print a page, click the button, and be ready to print the next page.

      • Max says:

        I adjusted the original script to add sequential numbers to set fields (I had ten).

        Thanks for the post this really spend up a slow process (manual numbering forms) at office.

        //set the vars
        var one = this.getField(“1”);
        var two = this.getField(“2”);
        var three = this.getField(“3”);
        var four = this.getField(“4”)
        var five = this.getField(“5”);
        var six = this.getField(“6”);
        var seven = this.getField(“7”);
        var eight = this.getField(“8”);
        var nine = this.getField(“9”);
        var ten = this.getField(“10″);
        //this function that checks if the destination field is blank and if so popluates it.
        function checkAndPopulate(orig,populate){
        if(populate.value==”||populate.value==null){populate.value=orig.value+1}
        }

        //copy value of one to two, three and four
        checkAndPopulate(one,two);
        checkAndPopulate(two,three);
        checkAndPopulate(three,four);
        checkAndPopulate(four,five);
        checkAndPopulate(five,six);
        checkAndPopulate(six,seven);
        checkAndPopulate(seven,eight);
        checkAndPopulate(eight,nine);
        checkAndPopulate(nine,ten);

    • Val Lovelace says:

      Thanks, you saved me this evening! This works perfectly!

    • Marsha Warren says:

      Can I use 2 drop down list to concatenate a third field. Then based on that value populate the field with data from a field. or table. Or Can I insert a table the based on the cell return a value?

    • Berni says:

      Hello, great instructions Thank you. I am attempting to create an auto fill in one field box that results from selecting an item in a previous drop down field box. ie: Field 1 = Supplier Name Field 2 = delivery cost. and depending on which supplier name is chosen then their corresponding delivery cost will be automatically filled in.
      Thank you in advance,
      Berni

    • Quinton Irons says:

      Brilliant thank you. How would I go about taking the value from a set up radio buttons (each has a value attached e.g. wedding, party, etc) when a certain radio button is clicked it will fill a text field with the value of the radio button. Having no joy with it.

    • Pam Moses says:

      I have javascript to take the value of field 1 and move it to field 2. My problem is if the user updates field 1, field 2 is not updated and keeps the original value. I can’t seem to get the contents of field 2 to refresh after an update. Am I missing something?

    • Eric L says:

      I am new to Javascript but have managed to complete a number of pdf sales forms some quite complex including populating an empty field when another field is populated. I thought I was getting the hang of things but this has me stumped. I need to produce a simple sales form which has a price field that changes its price when the date changes. Example: if date is 20160213
      then “price” =£12.00. I used the yyyymmdd format as I thought this would represent a value but apparantly this is not so. Please help

    • Eric L says:

      Something went wrong with my last posting, some of it did not transmit.
      it should have read
      Example: if date is less than or equal to 20160213 the the “price” = £12.00
      If date is more than 20160213 then the price is 14.00

      Eric

    • ines says:

      I’m starting a very small business in complementary health. I’ve prepared a form for clients to fill out and it would be great if it could transfer field content to a place in the same file. It’s a little different to what’s described here.
      Could someone recommend a book so I can learn how to do this myself?

      Alternatively I’m happy to pay someone who can tell me how to do it.
      Kind regards

    • Crystal says:

      I am wondering if the population can happen outside of the adobe pdf file, for example, from an external file content or file name, and populate the fillable pdf form field? any idea on it?

      • vid@uoregon.edu says:

        Yes, there are ways to inject data into forms. For example, some folks use their web server and a specialized function or class (like PHP and ‘fpdf’) to inject data into PDFs. A Google search may reveal the combination that would work for you.

        • William Lamb says:

          I am a teacher and was wondering how I would take a number and make it translate to how many points it would be worth in another text field. For example, one of the evaluations that I give has a possible 300 point score and would translate into a 100 percent toward the student’s GPA. How would I go about doing that?

          • vid@uoregon.edu says:

            Hi William;

            You can perform calculations on the data. The adobe forums had some good examples:
            https://forums.adobe.com/thread/1956661
            event.value = (5 +foc+misc+abi+(Math.floor(lvl/2)));}

            and
            https://forums.adobe.com/thread/1965875
            event.value = “We agree to pay the participating broker(s), ” + d1 + ” and ” + d9 + “, a commission in the amount of: $” + util.printf(“%,0.2f”, Number(totalcommission)) + “.”;

            _________
            You can see in the above snippets that values are being added, divided and even formatted.

            Follow the links for more info.

    • Patrick says:

      Hi, I think this is the Javascript I am looking for.

      In Adobe I have a ‘Clients name’, when I enter this name it automatically populates two fields which are required. I also have a third column but sometimes isn’t required. How do I get the ‘Clients name’ to appear when required without re-typing? Is there a script for example if I click ‘Coloumn3’ the clients name will appear?

    • Worth Beacham says:

      Just checking but is it possible to set the Var’s to equal the same destination.

      Like
      Name
      Phone
      Email

      All in separate Text boxes, but they Combine in the destination box.
      I’m running into a wall getting this to work.

    • Jerry Penny says:

      New to Acrobat DC Pro and JavaScript. Have programmed with other languages extensively years ago, mainly database systems design. Lots of Vbe, and SQL.

      I have a form that I am converting to fillable and want to assign the value of a date formatted field(MM) with the value from another date formatted field(MM) – seems that when you prepare the form, and the original designer of the form placed text like Date:_____/_____/_____ on the form, you get three separate field there. Lets call field1 Date_month, field2 Date_day, and field3 Date_year. Each field gets formatted as a date field with wither mm, dd, or yyyy with the character places limited accordingly.

      I place an onBlur Action JavaScript similar to the following code for each field..:

      // onBlur action(When you enter a month) for the “Effective_month” field:

      if(event.value != “”);
      {

      // populate Signed_Date_month field with the value of Effective_month;

      this.getField(“Signed_Date_month”).value = this.getField(“Effective_month”).value;
      }

      When my cursor leaves Effective_month with a valid month number, I get an error stating invalid date/time format.

      Maybe I have been away from coding for too long, but this looks like it should work just fine.

      Can anyone tell me what I am doing wrong? Thanks.

    • vid@uoregon.edu says:

      Hi Jerry;

      I think you’re close; I looked into it and you have to pre-format the value as a date when you set it.

      I tried this and had to use util.scand() to read in date info and util.printd() to output a date value.
      Ex:

      date1full = util.scand(“yyyy-mm-d”,date1raw);

      two.value=util.printd(“mm”, new Date(date1full));

      Here was my full working code for a form w/ 2 fields date1 & date2; both of type date w/ the format “mm”:
      //Set the vars one and two:
      var one = this.getField(“Date1”);
      var two = this.getField(“Date2”);

      //get today’s date:
      var today = new Date();

      //Create a date (yyyy-mm-d) current year, user month input (date1), first day of the month:
      var date1raw = today.getFullYear() + ‘-‘ + one.value + ‘-01’

      //convert the date strings into Date Objects using util.scand(). Ref: https://acrobatusers.com/tutorials/date_time_part2
      var date1full = util.scand(“yyyy-mm-d”,date1raw);

      //next check if two is blank and if so, populate it with one’s value, in “mm” format
      if(two.value==”||two.value==null){
      two.value=util.printd(“mm”, new Date(date1full));
      }

      This page was helpful regarding Date Objects in acrobat: https://acrobatusers.com/tutorials/date_time_part2

      I created an ex. PDF incase that’s helpful:
      https://blogs.uoregon.edu/developments/files/2010/07/concat_javascript_test-date2-18bam7x.pdf

    • K.C. Murphy says:

      Thanks so much for this great script! However, when I do it, all the blanks I want to auto-fill just say “[object Field]”? What am I doing wrong?

    • Marjorie D Thompson says:

      Hi! I want to populate a textbox with what is selected in a listbox, but separated by commas. What script would I need for that and where would I put it? The Listbox is titled Listbox22, text box is titled Interventions.

      Any help would be greatly appreciated!!

    • Mike Hayes says:

      Greetings. I’m creating an Acrobat DC (pdf) school registration fillable form. What action and scripting do I need to populate identical address information for mom and dad when a checkbox is selected (see below). The function I’m looking to copy is akin to how online businesses auto-fill a billing address when a customer (after completing their mailing address) selects, “☐ Billing and Mailing addresses are the same” (or similar wording).

      Mother Father (☐ same address as Mother)

      Address Address
      City City
      State State
      Zip Zip

      Next question, what action and scripting are needed to allow parents to skip info for work (business, address, phone, etc.) if they select “☐ not working” (see below)? Work information and associated entries are required unless the parent is not employed (stay at home mom or dad, etc.).

      Simply stated, how can the form be coded so that these two rules apply: Box unchecked, entries required; Box checked, no entries required?

      Mother Work (☐ not working) Father Work (☐ not working)

      Business: Business:
      Address: Address:

      Thanks! /Mike

    Leave a Comment