Skip to main content
Inspiring
Jan 9, 2023 11:40 PM UTC
Answered

Show/Hide Multiple Field Groups In Progression Based on Dropdown Box Selection

I think my question on a previous thread got buried. The answer I need has also changed slightly so I'm starting a new thread in hopes that I'm able to get clarification.

I have a dropdown with 10 options, but I need the number of field groups to become visible based off of the user's selected number in the dropbox. For example, if "1" is selected from the dropdown, then I need all the fields in field group "SDP1" to be visible and the other 9 groups to be hidden. If "2" is selected, then I need all fields in groups "SDP1" and "SDP2" to be visible while the other 8 groups are hidden. If "3" is selected, then I need all fields in groups "SDP1", "SDP2", and "SDP3" to be visible while the other 7 groups are hidden. I need this progression all the way up through "SDP10".

This topic is closed to new replies. Start a new post to keep the conversation going.
Craig22304618iz91Correct answer
Inspiring
Jan 27, 2023 7:13 PM UTC

Arrg!! Second time I've done that. 

Here's the fix:

//First hide all field groups
var nIdx = Number(event.value);
if(!isNaN(event.value) && (nIdx > 0)){
   for(var i=1;i<=10;i++){
      this.getField("SDP." + i).getArray().forEach(function(a){a.display = (nIdx <= i)?display.visible:display.hidden});
   }
}

This is awesome! I changed "(nIdx <= i)" to "(nIdx >= i)" and now it works perfectly. I really appreciate your time and effort you put into helping me.Thank you

14 replies

Thom Parker
Community Expert
Community Expert
Jan 10, 2023 12:24 AM UTC

Read this article

https://www.pdfscripting.com/public/Hiding-and-Showing-Form-Fields.cfm?sd=40

 

Use a validation script on the dropdown to perform the action:

 

For Example

 

//First hide all field groups
for(var i=1;i<=10;i++)
   this.getField("SDP" + i).display = display.hidden;

var nIdx = Number(event.valueAsString);
if(!isNaN(event.valueAsString) && (nIdx > 0))
   this.getField("SDP" + nIdx).display = display.visible;
Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
Jan 17, 2023 5:52 PM UTC

Hi Thom. Thanks for the help. I put the script you provided into the "Run custom validation script" and selected the "Commit selected value imediately" box in the dropdown menu. My field names are SDP.1.Title, SDP.1.Cur, SDP.2, etc. I read the link you provided and it was very helpful, but it's still not working. Is there someting that needs to be done in the fields? What am I missing? 

Translate
Thom Parker
Community Expert
Community Expert
Jan 17, 2023 8:14 PM UTC

So the first thing to do when somethings not working, is to look in the console window to see if any errors are reported. 

In this case an "InvalidGetError" would have been reported.  

So when you post about something not working the first thing you should do is describe how it's not working. Is there a change or not? If there is a change, then what is it. And restate the expected behavior. Then post any errors messages. 

 

Here's the corrected code. I did not include an important bit, i.e., the iteration over the fields in the group. This is not always necessary, but in this case it is.  

 

 

 

//First hide all field groups
for(var i=1;i<=10;i++)
   this.getField("SDP" + i).getArray().forEach(function(a){a.display = display.hidden});

var nIdx = Number(event.valueAsString);
if(!isNaN(event.valueAsString) && (nIdx > 0))
   this.getField("SDP" + nIdx).getArray().forEach(function(a){a.display = display.visible});

 

 

 

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