Skip to main content
Participant
July 27, 2021
Answered

Show/Hide layer field based on dropdown box selection

  • July 27, 2021
  • 1 reply
  • 3129 views

Hi there, I have a form with three layers. The full form is on one layer and layer 2 and layer 3 have additional form fields that I want to displayed depending on the choice made in the dropdown. Can anyone please assist me? I have very basic Javascript skills. Much appreciated!

 

Correct answer Shawn21800473kqsz

Seems fine to me... Are you sure all the layer names and the values of the drop-down field are EXACTLY correct? Even a single space in the wrong place, or a lower-case letter instead of an upper-case one will cause it to malfunction.

If you still can't get it to work, share the file for further help.


Thank you for the help! I found the error. There was indeed an extra space when I created the layers in indesign that I didn't copy correctly into the code. 

1 reply

try67
Community Expert
Community Expert
July 27, 2021

You can use this code as the custom Validation script of the drop-down field to show "Layer 2" when "Option 2" is selected (and hide it otherwise) and "Layer 3" when "Option 3" is selected:

 

 

var ocgArray = this.getOCGs();
for (var i=0; i < ocgArray.length; i++) {
	if (ocgArray[i].name=="Layer 2") ocgArray[i].state = (event.value=="Option 2");
	else if (ocgArray[i].name=="Layer 3") ocgArray[i].state = (event.value=="Option 3");
}

 

 

Note, however, that the visibility state of layers is not saved with the file, so if these layers are set to be hidden by default and the user selected one of these two options (which caused the matching layer to appear), and then save the file, closed it and re-opened it, they would both still be hidden. In order to fix that you would need a more complex solution where the visibility state of the layers is saved to a text field (or a metadata property) when the file is saved, and then read and applied when it is opened.

 

Edit: fixed a small mistake in the code

Known Participant
August 9, 2022

Hello Try67,

I have a similar situation where I have a dropdown selection of five different answers to a question, and if any of the last three are selected, then it is to show the layer. I have tried the code in two ways: "||" between the selections or "if else". Neither of them works. I am still a greenhorn at Scripting in Acrobat but improving.

 

var ocgArray = this.getOCGs();
for (var i=0; i < ocgArray.length; i++) {
if (ocgArray[i].name=="Layer 2") ocgArray[i].state = (event.value=="Acceptable"||"Needs Improvement"||"Poor");
}

 

var ocgArray = this.getOCGs();
for (var i=0; i < ocgArray.length; i++) {
if (ocgArray[i].name=="Layer 2") ocgArray[i].state = (event.value=="Acceptable");
else if (ocgArray[i].name=="Layer 2") ocgArray[i].state = (event.value=="Needs Improvement");
else if (ocgArray[i].name=="Layer 2") ocgArray[i].state = (event.value=="Poor");
}

try67
Community Expert
Community Expert
August 9, 2022

Neither of your codes will work correctly. Try this:

 

if (ocgArray[i].name=="Layer 2") ocgArray[i].state = (event.value=="Acceptable" || event.value=="Needs Improvement" || event.value=="Poor");