Conditional Field Script
Copy link to clipboard
Copied
Hi,
I have a field on an Adobe Acrobat Pro DC form that I want to show or hide based on the value of another field.
My Conditional Field is Field A.
My Controlling Field is Field B.
I only want to show Field A when Field B = X.
Otherwise, Field A is hidden.
Is there some script I can write in my form to accomplish this?
If so, can someone help me out with the code for the script? And tell me exactly where I should copy/paste this script into the form?
I appreciate the help.
Copy link to clipboard
Copied
The custom validation script for Field B could be:
// Custom Validation script for Field B
getField("Field A").display = event.value === "X" ? display.visible : display.hidden;
where "Field A" should be replaced with the exact field name of that field. Note that the script won't execute until the value of Field B changes, but once it does it will control the visiblity of Field A as expected. Enter the script on the Validate tab of the Field Properties dialog.
Copy link to clipboard
Copied
Hi,
Thank you for the response. I copied the code as you indicated but it did not hide the field. I uploaded photos of my Field A and Field B. Including the validation script for Field A.
I only want Field A to show if Field B = 1. But Field A shows for any value of Field B.
Can you do me a favor and look over my code and tell me whether I have it correct.
Thanks again for the help.
Below are screen shots of Field A General Tab, Field A Validation Tab, and Field B General Tab.
Field A - General Tab
Field A - Validation Tab
Field B - General Tab
Copy link to clipboard
Copied
Put the code in Field B.
Copy link to clipboard
Copied
Hi, Yes this worked on my source document! Thank you.
However, my PDF is being pushed out of a database program that merges the fields. And it does not work on the PDF that is generated from my database program?
George_Johnson mentioned "the script won't execute until the value of Field B changes". Perhaps this is the reason? My database program is merging a static value into Field B. But I would still like this static value to control the value in Field A. Is this possible?
Copy link to clipboard
Copied
If the data in the field is being populated outside of Acrobat/Reader (or some other PDF viewer that supports the scripting used) then it won't work.
Copy link to clipboard
Copied
In that case you should move the code to be the calculation script of the field, and add the following code as a doc-level script (under Tools - JavaScript - Document JavaScripts):
this.calculateNow();
When you open the file it will force all the fields to re-calculate and your script will execute.
Copy link to clipboard
Copied
Hi,
I did as suggested and it works great on the source document. However, when I merge the form from my database program the problem still persists. Field A is shown for all values of Field B. Field A will not hide.
Below is a screen shot of how I entered the code. Did I input it correctly?
Document Java Script
Copy link to clipboard
Copied
Yes, that is correct. It should work...
Copy link to clipboard
Copied
George's script is exactly the right way to implement the show/hide functionality for a text field.
You can find more details (more complete info) on showing and hiding any kind of field here:
https://www.pdfscripting.com/public/Hiding-and-Showing-Form-Fields.cfm
Use the Acrobat JavaScript Reference early and often

