Skip to main content
Inspiring
October 24, 2022
Answered

Multiple Radio Button Groups "Other" Selection shows/hides single "Other" text box.

  • October 24, 2022
  • 2 replies
  • 1869 views

Ok, so I have multiple radio button groups with an "Other" selection.  I am attempting to make a single "Other" text box visible/hidden based on any of the varying radio button groups selecting the "Other" option.  Here is what I've attempted to work out and I'm clearly not understanding a whole lot when it comes to arrays. I have the below in the custom calculation script in the "Other" text box.

My sincerest thanks in advance for any and all assistance. 🙂

var Other = [];
other[0]= ("Incident_Health_System_Nervous");
other[1]= ("Incident_Health_DigestionNutrition")
other[2]= ("Incident_Health_UT")

other[3]= ("Incident_Health_Respiratory")

other[4]= ("Incident_Health_Circulatory")

var showField = this.getField("[0][1][2][3][4]").valueAsString=="Other";
if (showField) event.target.display = display.visible;
else {event.target.display = display.hidden; event.value = "";}

This topic has been closed for replies.
Correct answer Thom Parker

There is typo in the script, change: other.some to Other.some.


Good catch.  Here's the corrected code:

 

var Other = ["Incident_Health_System_Nervous","Incident_Health_DigestionNutrition","Incident_Health_UT","Incident_Health_Respiratory","Incident_Health_Circulatory"];

var showField = Other.some(function(cNm){return this.getField(cNm).value == "Other";});
if (showField) 
    event.target.display = display.visible;
else {
    event.target.display = display.hidden; 
    event.value = "";
}

2 replies

Thom Parker
Community Expert
Community Expert
October 24, 2022

There are several different ways this type functionality could be achieved. Since you are already using an array, here's a modification of your code that will work. 

 

var Other = ["Incident_Health_System_Nervous","Incident_Health_DigestionNutrition","Incident_Health_UT","Incident_Health_Respiratory","Incident_Health_Circulatory"];

var showField = other.some(function(cNm){return this.getField(cNm).value == "Other";});
if (showField) 
    event.target.display = display.visible;
else {
    event.target.display = display.hidden; 
    event.value = "";
}

 

If I was designing the form I'd do it slightly differently, by naming the fields differently. I'd use a group naming scheme.

Like this:

"IncidentHealth.System_Nervous", "IncidentHealth.DigestionNutrition", "IncidentHealth.UT", etc

Now all fields in the "IncidentHealth" group can be acquired with a single line

 

var aIHealthFlds = this.getField("IncidentHealth").getArray();

 

You can read more about form scripting techiques here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
October 24, 2022

Thank you for the assist.  That seems to have done the trick with one exception.  If more than three (3) "Other" radio-button's have been selected, the "Other" text field becomes hidden.  Irrespective of which "Other" buttons are toggled, the field disappears once four of them have been selected.  Trying to figure out if there are any limitiations to an array.  I wouldn't think five groups of radio-buttons excessive but as I've clearly proven, I'm not understanding something regarding arrays.

Thom Parker
Community Expert
Community Expert
October 24, 2022

There is nothing in the code that would be affected by the number of radio button groups that have "Other" selected. It's likely there is something else going on. Can you post the PDF file?

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
October 24, 2022

Are the radio-buttons in a single group? If so, what's the group's name?

 

Inspiring
October 24, 2022

No.  The radio-button groups are as listed above in my poor attempt at an array.  Each of the groups are listed above following the "Incident_Health" section of the form.  Unfortunately, for data collection purposes, "Incident_Health" also has two pulldowns and a few check boxes within its overall section..There are anywhere from 5-9 radio buttons for each of the radio-button groups listed -- i.e.  System_Nervous; DigestionNutrition; UT; Respiratory; Circulatory.

Thom Parker
Community Expert
Community Expert
October 24, 2022

So how did the code I provided work? with the form as is. 

I only mentioned using a different methodology as a design tip.  

 

 

 

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