Show/Hide layer field based on dropdown box selection

New Here ,
Jul 27, 2021 Jul 27, 2021

Copy link to clipboard

Copied

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!

 

TOPICS
PDF forms

Views

331

Likes

Translate

Translate

Report

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 ,
Jul 27, 2021 Jul 27, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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
Explorer ,
Aug 09, 2022 Aug 09, 2022

Copy link to clipboard

Copied

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");
}

Likes

Translate

Translate

Report

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 ,
Aug 09, 2022 Aug 09, 2022

Copy link to clipboard

Copied

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");

Likes

Translate

Translate

Report

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
Explorer ,
Aug 09, 2022 Aug 09, 2022

Copy link to clipboard

Copied

LATEST

Hello Try67,

Well, I found it after it dawned on me that you need the "eventvalue==" for each item with the '||" between them. I got it to work. Thanks for your post!

Likes

Translate

Translate

Report

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