Skip to main content
December 23, 2022
Answered

Hide Fields upon selection of drop down

  • December 23, 2022
  • 2 replies
  • 3295 views

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

This topic has been closed for replies.
Correct answer try67

Not getting any error messages. 

 

 


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.

2 replies

Participating Frequently
December 23, 2022
Hi
Bundle it share with github, application, and put represitory on it. Ask
for clear updates.
Thom Parker
Adobe Expert
December 23, 2022

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 PDFScriptingUse the Acrobat JavaScript Reference early and often
December 26, 2022

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.  

 

 

December 26, 2022

Are there any error messages when you use the script? Can you share the file in question with us?


Not getting any error messages.