show/hide fields based on drop down choices

Community Beginner ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

I have a dropdown field with three choices. Based on the selection, I need a specific text form field to appear, and the others to remain hidden. (Or if it makes the code more clean, the resulting field could be a dropdown, too, with the appropriate choice selected.) I don't know Java well enough to extrapolate code from online examples and repurpose it for my forms. I can provide a sample form. Thanks in advance for your help.

TOPICS
Acrobat SDK and JavaScript, PDF forms, Windows

Views

27.1K

Likes

translate

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

Engaged , Jun 11, 2014 Jun 11, 2014
There are some ways to execute this.  the easiest for you would be to add this script to the calculate (calculate tab in properties) event of the field that needs to be hidden.  Take notes that everything after "//" is just hints for you to understand the code.  You will need to put this script in every field that you want to replicate the behavior, changing the value according to what you want to accomplish.  the word "event" means myself for you to understand.if (this.getField("dropdown").valu...

Likes

translate

Translate

Translate
Engaged ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

There are some ways to execute this.  the easiest for you would be to add this script to the calculate (calculate tab in properties) event of the field that needs to be hidden.  Take notes that everything after "//" is just hints for you to understand the code.  You will need to put this script in every field that you want to replicate the behavior, changing the value according to what you want to accomplish.  the word "event" means myself for you to understand.


if (this.getField("dropdown").value == "put value here"){      //replace "dropdown" with the name of the dropfield

event.target.display = display.visible;

}

else{

event.target.display = display.hidden;

}

This solution works if you have a small amount of fields.  If you manage to have a large number of similar fields, let me know because there are other and faster ways.

Likes

translate

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 ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

I only have a few fields, so hopefully we can make this solution work. I did run into an issue, though.

On the field I want to show/hide, called "14", I entered the following in the "Calculate - Custom Calculation Script" field. "State#1" is the name of the dropdown field and "Pickles" is the choice within that dropdown field.

if (this.getField("State#1").value = "Pickles"){

event.target.display = display.visible;

}

else{

event.target.display = display.hidden;

}

Doing this resulted in the following error messages:

SyntaxError: missing ) after condition 1:

SyntaxError: missing ) after condition 2:

Not sure what to do from here?!? Thanks for your continued help.

Likes

translate

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
Most Valuable Participant ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

Change this:

if (this.getField("State#1").value = "Pickles"){

To this:

if (this.getField("State#1").value == "Pickles"){

Likes

translate

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 ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

Made that change, now I get the following error.


this.getField("State#1") is null
1:AcroForm:18:CalculateException in line 1 of function top_level, script AcroForm:18:Calculate

TypeError: this.getField("State#1") is null
1:AcroForm:18:Calculate

Is there a place I can post the file, if that would make it easier? Also, why does it need two "=" signs?

Likes

translate

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
Most Valuable Participant ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

That means there's no field in your file called "State#1". If you're getting this name from the fields list in Form Edit mode, the "#1" part just means that it's a copy of another field called "State", and that's the name you should use.

In JS, "=" is the assignment operator. "==" is the comparison operator.

You can post the file to a file-sharing website (dropbox, acrobat.com, etc.) and then post the link to it here.

Likes

translate

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 ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

Thank you, Gilad D. for the help and extra information, too.

Likes

translate

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
Engaged ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

also, if "==" could be roughly translated to "is", "!=" would mean "is not".

JavaScript is Case sensitive so make sure you did not name your field STATE#1 instead.  It's a pretty common error.  Try not to use special characters when naming fields.  Stick to standards.  I personnally always name fields with capital letters and numbers using identation like

MYFORM.PLACE.NUMBER

MYFORM.PLACE.ADDRESS

MYFORM.SEX.MALE

MYFORM.SEX.FEMALE

This is really useful if you have lots of fields related to one another.  You can then apply methods to entire groups of fields in one simple line of code.

For example this.getField("MYFORM").display = display.hidden would hide all four fields.  It works kind of the same way as folders unside your computer.

Finally, "#" is generally used as widget identification.  I think this is what happened.  If you copy a field, it retains the same name and some of its charateristics but acrobat will assign them a number called widget.  The original field State will get the identification State#0 in the right pane as it is 0-based.  The copie will be identified State#1.  You must understand that the name of the field is still "State".  Some methods (actions you can code) let you determine the widget number for targetting a specific field from those you copied.  It is not the case here.  So for disambiguation, you should name your fields different name like STATE1 and STATE2, or STATE.1 and STATE.2 if you plan to use what I wrote about earlier.

Likes

translate

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 ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

Thank you for the help and extra information. I was able to get it to work! I definitely need to learn JavaScript.

Likes

translate

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 ,
Oct 30, 2017 Oct 30, 2017

Copy link to clipboard

Copied

"This solution works if you have a small amount of fields.  If you manage to have a large number of similar fields, let me know because there are other and faster ways."

Can you explain these faster ways please?

Likes

translate

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 ,
May 31, 2018 May 31, 2018

Copy link to clipboard

Copied

I'd be interested in alternate ways. Specifically, I notice the Validation code doesn't run when a selection is made from a menu, unless the enter key is pressed. Selecting with a mouse does not trigger the code, nor does adding the code to a mouse Action, which only runs when the field is clicked. Not sure what Mouse Enter refers to, but didn't appear to solve issue.

Basically I want a dynamic field to change as selections are made by any means? Are there any events or actions I can trigger or consume?

Likes

translate

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
Most Valuable Participant ,
May 31, 2018 May 31, 2018

Copy link to clipboard

Copied

It will, if you tick the option to commit the selected value immediately, under the field's Properties, Options tab.

Likes

translate

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 ,
May 31, 2018 May 31, 2018

Copy link to clipboard

Copied

Thankyou, works perfectly. 🙂

Likes

translate

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
Engaged ,
May 31, 2018 May 31, 2018

Copy link to clipboard

Copied

You could use the setItems() method of the field object instead of hiding and showing fields.  The setItems() method will change the choices of a dropdown menu dynamically by inserting the elements of an array as choices

For example, let's say you have 2 dropdown fields named "food.type" and "food.name"

if (this.getField("food.type").value = "fruits"){

   this.getField("food.name").setItems([" ", "Apple", "Oranges", "Bananas"])

}

else if (this.getField("food.type").value = "vegetables"){

   this.getField("food.name").setItems([" ", "Potatoes", "Carotts", "cabbage"])

}

the choice you make in the first field changes what is available in the second field

Likes

translate

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
Most Valuable Participant ,
Jun 01, 2018 Jun 01, 2018

Copy link to clipboard

Copied

There are errors in your code. You used the value assignment operator instead of the comparison operator.

Likes

translate

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
Engaged ,
Jun 01, 2018 Jun 01, 2018

Copy link to clipboard

Copied

True.thanks

if (this.getField("food.type").value == "fruits"){

   this.getField("food.name").setItems([" ", "Apple", "Oranges", "Bananas"])

}

else if (this.getField("food.type").value == "vegetables"){

   this.getField("food.name").setItems([" ", "Potatoes", "Carotts", "cabbage"])

}

Likes

translate

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
Most Valuable Participant ,
Jun 02, 2018 Jun 02, 2018

Copy link to clipboard

Copied

Also, if this is the custom validation script of "food.type" then you should use event.value instead of this.getField("food.type").value .

Likes

translate

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 ,
Aug 08, 2020 Aug 08, 2020

Copy link to clipboard

Copied

This helped me as well! Thank you so much! Are you familiar how this can be adapted to accomodate  multiple values from one dropdown to trigger one form field to hidden. I tried to repeat the code for other values and does not function or will only function for the last value in the syntax. I am a complete novice so the fix can be easy, but I am not knowledgeble enough. Thank you in advance or any advice.

Likes

translate

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 ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

So I got it to work, then realized that what I really needed is for a specific value to appear in another form field, based on what is selected in the dropdown. Using my previous example, if the choices in the dropdowns are "WA" and "OR", when "WA" is chosen, I need "18" to appear in a different field and if "OR" is chosen, I need "14" to appear. I hope this makes sense, and again I appreciate any help that you can provide.

Likes

translate

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
Most Valuable Participant ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

You can use this as that field's custom calculation script:

var v = this.getField("Dropdown Field Name").value;

if (v=="WA") event.value = "18";

else if (v=="OR") event.value = "14";

else event.value = "";

Of course, you'll need to adjust the name of the drop-down field...

Likes

translate

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 ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

Thank you so much! Part of me wishes I'd realized what I needed from the outset, but now I have another bit of code that I may be able to utilize.

Likes

translate

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
Engaged ,
Jun 12, 2014 Jun 12, 2014

Copy link to clipboard

Copied

Values of "14" and "18" are different than values of 14 and 18.  The reason behind this is numbers alone are interpreted as numbers while numbers between "" are interpreted as a strings of character.  Maybe it doesn't change anything right now but it's good to know the difference.  Let's say you want to make calculations with these numbers, they need to be numbers, not strings.

Luckilly, when you retrieve a value from a field using the getField() method, javascript will interpret it in the most logical way (numbers being numbers) but maybe it's not what you need.

To force a string interpretation, you will use .valueAsString instead of .value OR put the getField() method inside this -->  String(put it here).  The same thing goes with numbers Number(put it here).

You need to be aware of the return value of the methods you use because sometimes, it switches between string, number, array, bollean.

Likes

translate

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 ,
Jun 13, 2018 Jun 13, 2018

Copy link to clipboard

Copied

I have a form with 4 options that dictate the visibility for 75 fields each. You mentioned a different method to control visibility for a large number of fields? I'm using Acrobat Pro DC.

Likes

translate

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 ,
Feb 06, 2019 Feb 06, 2019

Copy link to clipboard

Copied

I came across this thread looking for a similar answer on this myself.  Is there an easier way to set this up for multiple fields?  For instance, they first dropdown decision determines the second field, which determines the third, and so on.

Likes

translate

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
Most Valuable Participant ,
Feb 06, 2019 Feb 06, 2019

Copy link to clipboard

Copied

This is called "cascading" and is possible, but requires a more complex script and data model.

If you're interested, I've developed a (paid-for) tool that allows you to set it up easily, based on a spreadsheet with the data, and without having to write any code. You can find it here: Custom-made Adobe Scripts: Acrobat -- Create Cascading Dropdowns

Likes

translate

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