Create a dynamic dropdown field in Adobe Acrobat Pro XI

Explorer ,
Oct 30, 2014 Oct 30, 2014

Copy link to clipboard

Copied

Hello all. I am trying to create a field in one of my forms using Adobe Acrobat Pro XI. I have a dropdown list of "main" items, and based on what is selected in that list, I want a second dropdown list to give different choices. I have searched and searched and have javascript that I thought might work, but maybe I am have a mistake or I am placing the javascript in the wrong place, becasue it is not working. I know very, very little about javascript, so any help would be so much appreciated.

I am using Adobe Acrobat Pro XI. I added a "new field", and selected "dropdown". The field name is ProvType. The dropdown options are:

     Inpatient

     Outpatient

     Physician

I have the "Commit selected value immediately" selected.The field is not validated. The format is "none." There are no actions or calculations associated with it.

I then created another dropbox, and named it SubProvType. Under Actions, I selected the trigger as Mouse Up, and then selected the Action "Run a JavaScript." I selected the add button, and typed this in the JavaScript editor:

switch (ProvType.rawValue)

{

    case "Inpatient":

        this.setItems("Hospice,Hospital,Nursing Facility");

        break;

    case "Outpatient":

        this.setItems("Adult Day Center,Home,Other");

        break;    

    case "Physician":

        this.setItems("Surgeon,Family Practice,Neurologist");

        break;    

}

What I want to happen is:

If "Inpatient" is selected, I want the following options to be available in the second dropdown box "SubProvType":

     Hospice

     Hospital

     Nursing Facility

If "Outpatient" is selected, I want the following options to be available in the second dropdown box "SubProvType":

     Adult Day Center

     Home

     Other

If "Physician" is selected, I want the following options to be available in the second dropdown box "SubProvType":

     Surgeon

     Family Practice

     Neurologist

However.... when I close the form editing and try to select a different option in the ProvType field, the SubProvType field remains blank and there are no options available. There are also no errors.

I must be missing something, but I have no idea where to start. Thank you in advance for any help anyone can provide.

TOPICS
PDF forms

Views

52.3K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Oct 30, 2014 Oct 30, 2014
Use this code as the custom validation script of ProvType (and remove any code you associated with SubProvType): switch (event.value) {     case "Inpatient":         this.getField("SubProvType").setItems(["Hospice,Hospital,Nursing Facility"]);         break;     case "Outpatient":         this.getField("SubProvType").setItems(["Adult Day Center,Home,Other"]);         break;       case "Physician":         this.getField("SubProvType").setItems(["Surgeon,Family Practice,Neurologist"]);         br...

Likes

Translate

Translate
Adobe Community Professional ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

There's a lot of good advice in this thread. Did you read all of it? Did you follow the tutorials linked to? Did you try to adjust the code provided for your needs? If you don't want to mess around with it yourself you can always try the tool I've developed, which allows you to set it up without having to write any code. I'm also happy to set it up for you, for a small fee. You can contact me privately (try6767 at gmail.com) to discuss it further.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

Thankfully I found this thread, and it has been very helpful. I am running into a few issues and am wondering if you can help.

 

I have a form with 2 dropdowns, I successfully added validation script to the first to populate the second drop down. Now I want the user to choose their option from the 2nd drop down and have a new field show the suggested giving amount.

 

Scenario: Drop Down #1 - "Sector" - 8 options to choose from

Drop Down #2 - "Production Level" - Depending on what they chose in "Sector", up to 7 options to choose from.

 

I have it working so that the correct options show when user chooses their Sector, the correct "Production Level" options display, with one problem. After I choose an option in "Sector" the first choice for "Production Level" for that "Sector" shows. If I change the "Sector" type, it does not change until I click in that field. How do I make the fields update as changes are made?

 

Now I want to add a "Suggested Giving" field, I used a "text" field. I went back to the "Production Level" field properties, and added the following script in the "Run Custom Validation Script":

 

switch (event.value) {

case "<1,000 acres":

this.getField("Suggested Contribution").value = 250.00;

case "1,001 - 100,000 acres":

this.getField("Suggested Contribution").value = 750.00;

case "100,001 - 500,000 acres":

this.getField("Suggested Contribution").value = 1500.00;

case ">500,001 acres":

this.getField("Suggested Contribution").value = 3000.00;

case "<10 MMBFT/YR":

this.getField("Suggested Contribution").value = 600.00;

}

I then created the "Suggested Contribution" text field, and it seemed to work, but now no matter what I choose in the "Sector" and "Production Level" fields, the "Suggested Contribution" field is not calculating. There is an amount of $600 showing in the field that does not change no matter what I choose.

 

Any help?? Thank you in advance!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

You have to add a break command at the end of each case.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

I miss the break statements in your switch :

https://www.w3schools.com/js/js_switch.asp 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 21, 2018 Mar 21, 2018

Copy link to clipboard

Copied

Hi. This has been really useful, a big thanks to all the contributors.

I have a text field called "State" and a dropdown field called "deductible'

deductible has the following options "$7,500", "$5,000", "$2,500"

I would like "deductible" to provide "$7,500" & "$5,000" as options IF "MI" is entered in 'State"

And to provide "$2,500" IF any of the other 50 states are entered

And to provide "" IF "State" is = ""

I've used the following code, that works, (using a dropdown box for "State"), But I'd really like to use a text box if possible.

Also, do I have to have a line for each "state"option (50 lines), or can I combine them? i.e. case "AL", "AK", "FL" etc.

switch (event.value) {

    case "MI":

        this.getField("ded1").setItems(["$5,000","$7,500"]);

        break;

    case "MO":

        this.getField("ded1").setItems(["$2,500"]);

        break;   

    case "":

        this.getField("ded1").setItems([""]);

        break;   

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 21, 2018 Mar 21, 2018

Copy link to clipboard

Copied

That code will work for a text field, too. Just use it as the field's custom validation script.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 21, 2018 Mar 21, 2018

Copy link to clipboard

Copied

Thanks, yes, it works for a textbox

how do I list all of the states though?

this doesn't work

case "MO","FL", "AL" (etc)

Can I use?

case <> "MI" ? (obviously, my syntax is wrong, but you get my drift.

Basically if you select MI you have option 1 & 2, any other state you only have option 3.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 21, 2018 Mar 21, 2018

Copy link to clipboard

Copied

Use this:

switch (event.value) {

    case "":

        this.getField("ded1").setItems([""]);

        break;

    case "MI":

        this.getField("ded1").setItems(["$5,000","$7,500"]);

        break;

    default:

        this.getField("ded1").setItems(["$2,500"]);

        break;

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 06, 2018 Apr 06, 2018

Copy link to clipboard

Copied

That works perfectly. but not always. Never on a Mac (surprise, surprise), but not always on adobe reader either. I tried putting it on my website, for people to complete, but this formula doesn't work there either (chrome or edge).

I have a form that has several calculated fields, it works fine in Acrobat Pro, but very hit and miss in other programs. The form is an insurance application and it calculates the premium dependent on the "State" and the "Deductible" ("ded_1"). In Mac, the whole form seems broken, not just the formulas, but the layout too.

I'm having issues with one particular field "ded1".

The options of a drop down list field "ded1" is dependent on the data input of another field "State1". i.e. you input 'mi' it changes it to 'MI' and sets the options in 'ded1' to $5,000 & $7,500. you input any other state it converts it to upper case and sets 'ded1' to $2,500

I've placed this code in the 'Validate' tab of the 'State1' field.

event.value = event.value.toUpperCase();  // Sets the entry to upper case.

// Sets the options for the deductible field.

switch (event.value) {

    case "":

        this.getField("ded1").setItems([""]);

        break;

    case "MI":

        this.getField("ded1").setItems(["$5,000","$7,500"]);

        break;

    default:

        this.getField("ded1").setItems(["$2,500"]);

        break;

}

The pdf can be viewed here

https://www.statesideapm.com/bi-application-pdf

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 20, 2019 May 20, 2019

Copy link to clipboard

Copied

I am trying to use the code that try67 shared to populate a certain list of options to a dropdown box based on what is entered in another field. I am using the following code but cannot figure out when I go to select one of the options in the dropdown, it won't change it from the default.

switch (event.value) { 

    case "Inpatient": 

        this.getField("SubProvType").setItems(["Hospice,Hospital,Nursing Facility"]); 

        break; 

    case "Outpatient": 

        this.getField("SubProvType").setItems(["Adult Day Center,Home,Other"]); 

        break;    

    case "Physician": 

        this.getField("SubProvType").setItems(["Surgeon,Family Practice,Neurologist"]); 

        break;    

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

Each item needs to be in a separate string, like this:

setItems(["Hospice","Hospital","Nursing Facility"])

The first item in the list will always be selected by default.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

Thank you, but for whatever reason when the new list is populated in the drop down, I am unable to change from the default and click a new item in the list.

For example,

"Hospice" would appear as the default when "Inpatient" is selected. But I cannot change it to "Hospital" or "Nursing". It is just stuck on "Hospice".

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

Where did you place this code?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

I placed the code in the custom validation script of a text box where I would type either "Inpatient", "Outpatient", or "Physician" (in this case).

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

Then it should work fine... Can you share the actual file with us?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

I have attached a link. So in this case, I put the code in the text box next to "Job #" and made it so that when I type in 1907, the following list of cost codes appear in the 5 "Cost Codes" drop downs. But it will not let me click off of the default cost code.

https://documentcloud.adobe.com/link/review?uri=urn%3Aaaid%3Ascds%3AUS%3Ad922e1f0-17b2-4454-8bae-4ba...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

You don't need separate switch statements, put all the code into one switch.

And You need to remove the OnFocus and OnBlur code.  I mean you really need to remove this code.

The setItems function makes the first value the default value. You can't manually set the list value to " " because there is no " " entry in the list. If you want it in there is has to be in the list of items in the "setItems" function.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

Thom,

As far as putting the code into one switch, I tried to do that with all 5 of my separate "Cost Codes" dropdowns and the options would only appear in the first drop down, and not any of the other 4.

For the OnFocus and OnBlur code, I forgot that I had that there and thought for sure it would solve my problem, but when I erased it, I continued to have the problem of the default option not going away and not being able to select a new drop down option.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

Oh, forgot, that was the other thing. You code for setting the list items is in the Calculation script. So of course its run every time any field on the form changes. Which means the list items are constantly being refreshed, hence the issue with the selection. Move your code to the validate event, which is what you said you had done.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

THANK YOU this solved my problem!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 28, 2019 May 28, 2019

Copy link to clipboard

Copied

I ran into a new problem. I am trying to make it so that when the user enters a certain job number, a different list of cost codes appears for each job number.

I finished all of the lists of cost codes for job # 1803, but when I go to add a new list for 1907 with the code discussed, it gives me an error saying "SyntaxError: missing ] after element list". I know the code is correct because I have double checked it and have been copy and pasting the template for the code each time.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 28, 2019 May 28, 2019

Copy link to clipboard

Copied

Are all " correct?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 29, 2019 May 29, 2019

Copy link to clipboard

Copied

I made sure to type " inside the script so that it was the right format

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 29, 2019 May 29, 2019

Copy link to clipboard

Copied

There obviously is an error with your code, or you wouldn't have gotten that error message...

Can you post your code?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 29, 2019 May 29, 2019

Copy link to clipboard

Copied

Here is the code for when I type "1907" into the job number.

switch (event.value) { 

    case "1907": 

        this.getField("Cost Code2").setItems(["", "100050-0101", "101000-0101", "101400-0101", "103300-0101", "104000-0101", "107000-0103", "107300-0103", "107100-0106", "107400-0106", "107950-0107", "107000-0108", "107400-0108", "105200-0109", "200180-0109", "200420-0109", "107100-0110", "107500-0110", "200300-0201", "105100-0202", "200210-0202", "200420-0202", "200330-0203", "200320-0204", "105100-0205", "200260-0205", "200261-0205", "200420-0205", "200340-0207”, "200515-0208", "200516-0208", "200662-0209", "200661-0210", "200420-0301", "302000-0301", "302000-0303", "304000-0304", "302000-0305", "302000-0306", "302000-0307", "301000-0308", "200570-0309", "502000-0401", "502000-0402", "502000-0403", "502000-0404", "503000-0404", "502000-0405", "503000-0405", "502000-0406", "402000-0407", "402000-0408", "403000-0408", "402000-0409", "402000-0410", "404000-0410", "402000-0411", "200200-0412", "200570-0412", "502000-0413", "502000-0414", "301000-0415", "200570-0416", "200570-0417", "301000-0418", "702600-1001", "107100-1002", "107500-1002", "701800-1003", "200910-1004", "105400-1005", "200010-1005", "200420-1005", "105100-1006", "200210-1006", "200420-1006", "302000-1007", "302000-1008", "303000-1008", "302000-1009", "303000-1009", "302000-1010", "303000-1010", "105300-1011", "200110-1011", "200420-1011", "107100-1012", "107200-1012", "302000-1014", "106100-1015", "106100-1016", "501000-1017", "502000-1017", "503000-1017", "301000-1026", "200570-1027", "200661-1028", "200662-1029"]); 

        break;

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines