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

Hide Fields upon selection of drop down

Guest
Dec 23, 2022 Dec 23, 2022

Copy link to clipboard

Copied

Hello,

 

Need some help...I need to be able to hide several user input fields and some text fields when an option is selected from a drop down.  

 

A few of the Fields to hide are

this.getField("CY CU").value  // user entered field

this.getField("CY Plus CPI").value // user entered field

this.getField ("RY CU").value // user entered field

this.getField ("Expense type") // pre-filled text field that is read only is this still a .value 

 

I tried the below as a validation on each of the user entered fields , but the problem was it didn't hide until user entered data.  I don't want the user to enter data at all.  

var drop = this.getField("Contract type").valueAsString;
if(drop == "2") event.target.display = display.visible;
else if (drop == "4") event.target.display = display.hidden;

So now, I'm wonder if I need to put the validation text on the drop down field, I'm assuming it will hide the field when the option is selected.  But I'm not sure how I make the event.target all of those different fields if I put it there.  

 

Any guidance would be helpful as I haven't found the other posts to be helpful.

 

Clueless beginner

TOPICS
JavaScript , PDF forms

Views

1.2K

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 , Dec 26, 2022 Dec 26, 2022

In the Validation event, event.value doesn't return the Export Value, but the Display Value, so either adjust the code accordingly, or move it to the Calculation event. Also, you should tick the option to commit the field's selected value immediately (under Properties - Options), so that the script will execute as soon as you make a selection, not only when you exit the field.

Votes

Translate

Translate
Community Expert ,
Dec 23, 2022 Dec 23, 2022

Copy link to clipboard

Copied

Yes, you need to put the validation script onto the dropdown, because, it is the dropdown that is the source of the change that hides or shows fields.  

Just as a general rule, when an action is driven from a single source, then the code that implements the action needs to be triggered by that source.  

 

Of course your validation script will need to be changed because the "event" object will represent the dropdown. 

 

 

var oFld = this.getField("CY CU");
if(event.value == "2") oFld.display  = display.visible;
else if (event.value == "4") oFld.display  = display.hidden;

// Then repeat for other fields. 

 

 

 

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 Expert ,
Dec 23, 2022 Dec 23, 2022

Copy link to clipboard

Copied

You meant to write:

oFld.display = display.visible;

And:

oFld.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
Guest
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

Still can't get it to work, even with the correction.  Maybe some additional information will reveal something I'm missing.  So regarding the drop down that is driving this the 2 and 4 are the export values I assigned to the items in the list.  

 

The fields that I'm attempting to hide are currently formatted as visible.  The field also has a custom format JavaScript. 

 

I'm stumped and have been learning to write JavaScript as I go.  This form is my most complex to date.  And this is the last piece to figure out before I code the rest of the form.  I appreciate the assistance and insight.  

 

 

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 ,
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

Are there any error messages when you use the script? Can you share the file in question 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
Guest
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

Not getting any error messages. 

 

 

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 ,
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

In the Validation event, event.value doesn't return the Export Value, but the Display Value, so either adjust the code accordingly, or move it to the Calculation event. Also, you should tick the option to commit the field's selected value immediately (under Properties - Options), so that the script will execute as soon as you make a selection, not only when you exit the 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
Guest
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

Okay.  Ideally, I'll like to use the export value.  I'm just not sure how to do that.  I tried to get help with that a few weeks ago. 

So I adjusted the code to the words in the drop down and moved it to the custom calucation script, but it still doens't hide the field.  I need it to make the field "CY CU" not visible at all.  

 

Again, I'm new at this.  I'm getting better with each new challenge. 

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 ,
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

No, you have to do either one or the other. If you move the code to the Calculation event leave it as it was. If you edit it to have the Display Values, leave it under Validation. If you do both it still won't work...

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
Guest
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

Okay so I updated it under validation to the actual words and it still doesn't hide the 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
Guest
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

LATEST

Finally got it work.  

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 ,
Dec 23, 2022 Dec 23, 2022

Copy link to clipboard

Copied

Hi
Bundle it share with github, application, and put represitory on it. Ask
for clear updates.

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