Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to get 2 layers, triggered by 2 separate drop down menus, to appear at the same time?

New Here ,
Apr 11, 2025 Apr 11, 2025

Hi All,

I currently have a drop down menu that uses the below formula (under Run Custom Validation Script) to pull-up different layers with pictures. When the name matching the layer is selected in the drop down menu, the picture/layer appears. When I select a different name, the previous picture goes away and the new picture appears, and if there's no matching layer name in the drop down list, no picture/layer appears at all. Perfect.

 

However, when I tried to use the same formula in a second drop down list, so that I could see BOTH the picture/layer of the first drop down and also the picture/layer of the second drop down at the same time, only the picture/layer of the second drop down appears.

 

I'm looking for the same functionality I had before; if I choose an option in the drop down that has a matching layer name, the picture should appear, and stay until another picture is chosen or there's no matching layer name in the drop down list. I just want that for 2 separate drop down lists 🙂  Thanks in advance for your help!

 

var ocgs = this.getOCGs();
if (ocgs) {
for (var i in ocgs) {
ocgs[i].state = (ocgs[i].name==event.value);
}
}

TOPICS
Create PDFs , How to , JavaScript , PDF , PDF forms
189
Translate
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 ,
Apr 11, 2025 Apr 11, 2025

Note that in your code all (OCG) layers are included in the loop. So the state of all layers is set, and only the one that matches the selection will be made visible.   

To use two or more dropdowns to select other layers to be visible you must include all dropdowns in any code that makes the layers visible. 

Here's a simple modification of your code. It needs to be done for both dropdowns.

var ocgs = this.getOCGs();
var otherDropDownValue = this.getField("OtherDropDown").value;
if (ocgs) {
   for (var i in ocgs) {
       ocgs[i].state = (ocgs[i].name==event.value) || (ocgs[i].name==otherDropDownValue);
   }
}

 

 

 

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

Translate
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
New Here ,
Apr 11, 2025 Apr 11, 2025

Thanks Thom,

Just to confirm, the only modification I need to make to the formula you entered is on the second line, where I'll write the name of my drop down menu in the quotations? And then I copy the same exact formula into the second drop down list as well, but again only changing the name of that drop down list on the second line, in quotations? 

 

If I have that right, after entering the code into just the first drop down, the pictures/layers did no go away when I selected another option within that drop down; they just kept stacking on top of each other. But then when I added the code into the second drop down menu, I had the same original problem; the picture/layer from the first drop down menu disappeared and I could only see the picture/layer(s) from the second drop down.  I'm doing something wrong, but I'm not sure what 🙂

Translate
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 ,
Apr 11, 2025 Apr 11, 2025

Yes, you have it correct.  I'm not sure the issue.  The "state" is set to true if either of the dropdown selections match the OCG name.   There must be something else going on for the states to stick when not selected. 

 

Can you post the form, or the exact code used in the dropdown validate event.  

Are the OCG states changes anywhere else in the PDF?

 

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

Translate
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
New Here ,
Apr 11, 2025 Apr 11, 2025

Thanks Thom,

Here is a shortened version of what I'm working on. It's behaving the exact same way as my real form. Please let me know if you're able to fix it on here and then I'm sure I could get my actual form to work as well 🙂 Thanks!

Translate
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 ,
Apr 11, 2025 Apr 11, 2025

You have the dropdown names backward.  Other dropdown means the other dropdown, so change "Inner" to "Model" and "Model" to "Inner". 

Translate
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
New Here ,
Apr 12, 2025 Apr 12, 2025
LATEST

That did it! Thanks so much everyone!! 🙂 

Translate
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 ,
Apr 11, 2025 Apr 11, 2025

That's because your script is looping through all layers and setting the states based on the result of (ocgs[i].name==event.value) (true or false) so they will all be turned off by the script except the one that matches the value of the dropdown.  Rewrite both scripts like this:

var ocgs = this.getOCGs();
if (ocgs) {
for (var i in ocgs) {
if (ocgs[i].name==event.value)
{ocgs[i].state=true}
}
}
Translate
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 ,
Apr 11, 2025 Apr 11, 2025

Use @Thom Parker 's script.  Mine doesn't turn off any layers.

Translate
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