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

Conditional Field Script

New Here ,
Sep 21, 2020 Sep 21, 2020

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.

TOPICS
PDF forms
3.0K
Translate
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
LEGEND ,
Sep 21, 2020 Sep 21, 2020

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.

Translate
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 ,
Sep 21, 2020 Sep 21, 2020

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 TabField A - General Tabexpand imageField A - Validation TabField A - Validation Tabexpand imageField B - General TabField B - General Tabexpand image

 

 

Translate
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 ,
Sep 21, 2020 Sep 21, 2020

Put the code in Field B.

Translate
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 ,
Sep 21, 2020 Sep 21, 2020

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?

 

 

Translate
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
LEGEND ,
Sep 21, 2020 Sep 21, 2020

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.

Translate
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 ,
Sep 22, 2020 Sep 22, 2020

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.

Translate
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 ,
Sep 22, 2020 Sep 22, 2020

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 ScriptDocument Java Scriptexpand image

Translate
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 ,
Sep 22, 2020 Sep 22, 2020
LATEST

Yes, that is correct. It should work...

Translate
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 ,
Sep 21, 2020 Sep 21, 2020

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

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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