Skip to main content
Participating Frequently
November 17, 2020
Question

script to group fields when a box is checked

  • November 17, 2020
  • 2 replies
  • 2651 views

After doing some research yesterday, I did notice that javascript should help me with this question but I have never written a script and I struggle with the concept when reading some directions as I have a form that I am making fillable and when you read their examples the questions do not show until the individual selects a box and checks it. On a form, most of the information is in front of them already so they will see it either way. I work for a Michigan intermediate school district (ISD) and currently I am making HR forms fillable. I have a question in section 1 of the attached file that has 2 checkboxes the person must select 1 or the other. Question A. is only a checkbox, but if the person selects B., then follow-up questions must be completed. How do I add a condition to lock B. checkbox and questions, if A.? If B. is checked, how do I group the questions so the person completing the form completes the required fields for B.?

 
 
This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
November 17, 2020

Rikki,  Change the check field names from "Option1.A" and "Option1.B" to just "Option1" for both fields. Then change the export values on these fields to "A"and "B".   This makes the check boxes mutually exclusive, so that only one can be checked. 

You can read a bit about check boxes and radio buttons here:

https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm

 

Next, remove the "MouseUp" document script. 

 

Now if you want the benificiary info to be hidden until the "Option" B check box is checked, you'll need to come up with a scheme to do this. One method is to place one or more markup rectangles over the benificiary area. The rectangle(s) should be read only  and the backgroud could be a translucent gray, so the user can see, but not touch the fields behind it. Then use the mouse up action on both checkboxes to set the visibility of the recangle appropiately (this field visibility will also need to be changed).   

 

This is a slightly advanced technique. But it is very effective, and works with all the better PDF viewers. Let me know if you'd like consulting on developing this and other features on your form. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
rikki0670Author
Participating Frequently
November 17, 2020

Thom,

Thank you for your response. You truly simplified the language so I could figure out what I needed to do.

I am struggling with the name change on the checkboxes because it keeps throwing an error. To fix this, I believe I will need to change it to Option2 of each. Let me know if that is wrong. The error I am receiving: "A field of a different type already uses this name. Please choose a new... 

I appreciate the offer to consult. I am actually trying to help someone else in our building and in the process trying to learn everything needed for her form to work. So I appreciate the offer but I will need to decline. I am fascinated by all of the things Adobe can do.

 

Thom Parker
Community Expert
Community Expert
November 17, 2020

You are correct. Acrobat will not let you change field to an illegal name.  So you have change it to something else first, then change it back.

 

Please take a look at www.pdfscripting.com. You'll find all kinds of  useful info. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
November 17, 2020

If would be helpful if you could share a non-secure version of this file... Also, this file doesn't contain any fields at all. So the first step would be to add them. And you should not be locking the second check-box when the first is clicked. That's very confusing for the user. They should just have the same name, so that they can't be ticked at the same time.

Do all of that and then we'll help you with the code to enable/disable the dependent fields based on the selection.

rikki0670Author
Participating Frequently
November 17, 2020

Thank you Try67...I didn't realize the form was secure.

I tried this script, but there must be much more I need to do for this to work after I renamed all of the boxes for B. Group2.

//hide all "Group2" Fields
this.getField("Group2").display = display.hidden;

I removed the script because the boxes I do have on the form were not showing up due to the script.

Form I need help with - I couldn't figure out how to attach without adding the form to my Google Drive.

 

try67
Community Expert
Community Expert
November 17, 2020

You should rename the "A" and "B" fields to have a single name (let's say it's "Option1"). Use "A" and "B" as the export values.

Then you could use the calculation script of any text field (can even be a hidden one) to show/hide the dependent fields, like this:

 

this.getField("Group2").display = (this.getField("Option1").valueAsString=="B") ? display.visible : display.hidden;