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

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

Views

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

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

...

Votes

Translate

Translate
Community Expert ,
Dec 16, 2022 Dec 16, 2022

Copy link to clipboard

Copied

What is the current behavior?  Are there any errors reported in the Console Window?  Where exactly was this script placed? 

 

 

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
Explorer ,
Jan 05, 2023 Jan 05, 2023

Copy link to clipboard

Copied

Hi MatLac

I am making a form with similar features. I tried your script and it worked perfectly but as you mentioned there is a faster way to manage a large number of fields. Please help me with that because I have so many fields to hide and display based on dropdown

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 ,
Jan 05, 2023 Jan 05, 2023

Copy link to clipboard

Copied

LATEST

The way to handle large numbers of fields is to use Group Naming.

Read the section in this article on Field Names and Watch the video

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

 

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

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

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

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

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

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

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

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

Copy link to clipboard

Copied

Can you offer any other assistance or advice other than trying to constantly peddle your (paid-for) site and code? Seems to be your go-to response for things like this. While it’s super that you created it, and I’m sure you’re really proud of it based on how often you copy and paste that same response, but for those of us trying to learn and teach ourselves JS, this does nothing for us, and doesn’t teach us anything “without having to write any 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 Expert ,
Feb 06, 2019 Feb 06, 2019

Copy link to clipboard

Copied

You didn't ask any specific question so I didn't give any specific answer... If you want to do it yourself, go for it. If you have problems writing the code I'll (or others) be happy to help you sort it out, but you seem to expect others to do your work for you, for free. That's not how these forums work, usually. If you don't know any JavaScript at all I would say this is too complex a task for a novice. Start with simpler projects and build your way up.

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

Copy link to clipboard

Copied

Here's a free article and example file that shows the technique

https://acrobatusers.com/tutorials/js_list_combo_livecycle

I've posted this link about a hundred times to this forum, cause it gets asked all the time. A bit more effort searching the forum would save us all some time.

You'll also find endless info on list programming here.

https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm

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

Copy link to clipboard

Copied

REMINDER: Please be kind when asking individuals for help or guidance. It is disheartening to see this kind of attitude toward individuals who are taking time to help advance other's skillset. Hoping you found your solution and definately hope you use "thank you" more frequently in your day-to-day.

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

Copy link to clipboard

Copied

hoping im not too late here, but i need help with a specific issue.

i have two drop down boxes that i want to make the same value by default, but i need to allow the second field to be editable without adjusting the main dropdown box.

Yes or No1 = Yes  (then Yes or No2 = Yes by default, but can be selected to no)

Yes or No1 = No (then Yes or No2 = No and only no)

Any help would be a appreciated. New to the Java script stuff and could really use all the help i can get.

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

Copy link to clipboard

Copied

askytola  wrote

hoping im not too late here, but i need help with a specific issue.

...

Any help would be a appreciated. New to the Java script stuff and could really use all the help i can get.

Read the available material and post your question to a new thread.

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

Copy link to clipboard

Copied

i did read above material and even clicked the links you so kindly sent us to with the intent of getting an end user to spend 150$ a year on a membership for basic help. Given knowledge is worth paying for, but to fill a general help thread with what amount as advertisements is petty.

i can definitely open a new thread, but i posted in a thread that i felt most fit my situation after reading multiple different threads.

so please either help someone who is stuck and politely asking for assistance, or move on.

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

Copy link to clipboard

Copied

You can achieve it by using the following code as the custom validation script of your first drop-down field:

this.getField("Name of other field").value = event.value;

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

Copy link to clipboard

Copied

SERIOUSLY!!!! thank you. I know that had to be a "basic" question to anyone who is more than a novice, but the basic help doesn't go unappreciated. Again, seriously, 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 11, 2020 Feb 11, 2020

Copy link to clipboard

Copied

askytolaa  I'm sorry to hear you did not find the information at the links useful. In fact, everything was there in the free material and the free example to provide you with the solution had you taken the effort to read the material. This site is about helping people to find the resources they need. Sometimes that means helping debug a script or providing a little code. But we are not here to provide free work. 

 

You are very wrong about wwwpdfscriptingg.com, it is not about basic info. It provides a wealth of advanced information: Samples,Tutorials, Automation tools, Plug-ins, an On-line tools.  Much of the information you literally cannot find anywhere else.  Any one of the free tools is worth the membership price. 

In fact, a great deal of basic info is free on this site. Everything you needed to solve you problem is free. But it takes some reading and thinking to use. You just don't get everything handed to you for free. 

 

I believe you owe me an apologyy. Your post was rude and unfair. 

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 ,
Feb 10, 2020 Feb 10, 2020

Copy link to clipboard

Copied

I'm still struggling with this - I'm desperate. Everything is working EXCEPT for the red. I just cannot figure out why and I'm going crosseyed. That same exact action works in all other instances.

 

if (event.target.value == "AWARDS/PRIZES in accordance with Hospitality Policy") {

this.getField("REQUIRED Briefly describe the expense and its business purpose").display = display.hidden;  

this.getField("Check Box7").display = display.visible;  

this.getField("Check Box8").display = display.visible;  

this.getField("awards prizes").display = display.visible;  

} else {

this.getField("REQUIRED Briefly describe the expense and its business purpose").display = display.visible;

this.getField("Check Box7").display = display.hidden;  

this.getField("Check Box8").display = display.hidden;  

this.getField("awards prizes").display = display.hidden;  

}

 

if (event.target.value == "ENTERTAINMENT EVENT (tickets, recreational, sporting) in accordance with Hospitality Policy") {

this.getField("Check Box1").display = display.visible;  

} else {

this.getField("Check Box1").display = display.hidden;  

  

}

if (event.target.value == "ENTERTAINMENT SVCS for campus event (decor, equipment or venue rental, music) in accordance with Hospitality Policy") {

this.getField("ent svcs").display = display.visible;

} else {

this.getField("ent svcs").display = display.hidden;  

}

if (event.target.value == "FOOD/BEVERAGE/TABLEWARE in accordance with Hospitality Policy") {

this.getField("REQUIRED Briefly describe the expense and its business purpose").display = display.hidden;  

this.getField("food").display = display.visible;  

this.getField("students").display = display.visible;  

this.getField("off campus guests").display = display.visible;  

this.getField("employees").display = display.visible;

this.getField("spouse partner of employee").display = display.visible;  

this.getField("breakfast").display = display.visible;  

this.getField("lunch").display = display.visible;  

this.getField("dinner").display = display.visible;  

this.getField("light refreshments").display = display.visible;  

this.getField("campus wide").display = display.visible;  

this.getField("attendee list required").display = display.visible;  

this.getField("attach agenda").display = display.visible;  

this.getField("event name").display = display.visible;  

this.getField("benefit to the csu").display = display.visible;  

this.getField("why is meal necessary").display = display.visible;

this.getField("cost per person").display = display.visible;

} else {

this.getField("REQUIRED Briefly describe the expense and its business purpose").display = display.visible;  

this.getField("food").display = display.hidden;  

this.getField("students").display = display.hidden;  

this.getField("off campus guests").display = display.hidden;  

this.getField("employees").display = display.hidden;

this.getField("spouse partner of employee").display = display.hidden;  

this.getField("breakfast").display = display.hidden;  

this.getField("lunch").display = display.hidden;  

this.getField("dinner").display = display.hidden;  

this.getField("light refreshments").display = display.hidden;  

this.getField("campus wide").display = display.hidden;  

this.getField("attendee list required").display = display.hidden;  

this.getField("attach agenda").display = display.hidden;  

this.getField("event name").display = display.hidden;  

this.getField("benefit to the csu").display = display.hidden;  

this.getField("why is meal necessary").display = display.hidden;

this.getField("cost per person").display = display.hidden;

}

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 10, 2020 Feb 10, 2020

Copy link to clipboard

Copied

 

Is that really the name of the field? 

Are there any errors reported in the Console Window? In fact, to debug this, try running that single line of code in the console window.

You'll find a tutorial here on the console

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

If you're still having problems, post this question to a new thread.

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 ,
Feb 11, 2020 Feb 11, 2020

Copy link to clipboard

Copied

Yes, those are the field names. I tried debugger and it didn't return an error. I've posted a new thread. Thanks!

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

Copy link to clipboard

Copied

 @try67 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. I am a complete novice so the fix can be easy, but I am not knowledgeble enough. 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 ,
Aug 09, 2020 Aug 09, 2020

Copy link to clipboard

Copied

First, Read this article:

https://www.pdfscripting.com/public/Hiding-and-Showing-Form-Fields.cfm

 

You might also find it helpful to look through the material here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

Now, testing for a set of selections is just a matter of adjusting how the condition is written for the "if" or other type of conditional.

 

for example:

if((event.value == "a") || (event.value == "b") || (event.value == "c"))

   this.getField("OtherField").display = display.hidden;

else

   this.getField("OtherField").display = display.visible;

 

This code hides the "OtherField" on a selection of "a", "b", or "c". 

This could also have been done using a switch statement

 

switch(event.value)

{

     case "a":

     case "b":

     case "c":

         this.getField("OtherField").display = display.hidden;

         break;

     default:

          this.getField("OtherField").display = display.visible;

         break;

}

 

 

         

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