• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

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

65.5K

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

Community Expert , 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

...

Votes

Translate

Translate
Community Expert ,
Jul 13, 2017 Jul 13, 2017

Copy link to clipboard

Copied

In the last setItems command, remove the last comma.

Votes

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 ,
Feb 22, 2018 Feb 22, 2018

Copy link to clipboard

Copied

Hello,

I am new to script writing and I am seriously struggling.

I am trying to create a form in Acorbat where if one division is selected than the relevant position titles will generate in another dropdown. if someone could please give me a brief explanation on how to do this that would be greatly appreciated.

thank you,

Votes

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 Expert ,
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.

Votes

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!

Votes

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 Expert ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

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

Votes

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 Expert ,
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 

Votes

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;   

}

Votes

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 Expert ,
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.

Votes

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.

Votes

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 Expert ,
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;

}

Votes

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

Votes

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;    

Votes

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 Expert ,
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.

Votes

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".

Votes

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 Expert ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

Where did you place this code?

Votes

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).

Votes

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 Expert ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

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

Votes

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...

Votes

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 Expert ,
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

Votes

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.

Votes

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 Expert ,
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

Votes

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!

Votes

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.

Votes

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 Expert ,
May 28, 2019 May 28, 2019

Copy link to clipboard

Copied

Are all " correct?

Votes

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

Votes

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