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

Show/Hide Layers Using a Dropdown Menu

Community Beginner ,
May 07, 2024 May 07, 2024

Copy link to clipboard

Copied

I have a base layer that I want to remain visible, but I would like to switch between the rest of my layers by using a dropdown menu. I'm trying to write it such that when one layer is selected, the rest are hidden. This is what I have so far, but when I select anything from the dropdown, nothing changes.

 

// Define the layer names
var layerNames = ["Emergency Repair", "CSA Visit", "Installation", "Other"];

// Function to toggle layers based on dropdown selection
function toggleLayers() {
var selectedOption = event.target.value;
for (var i = 0; i < layerNames.length; i++) {
var layer = this.getOCGs()[layerNames[i]];
if (layer) {
if (layerNames[i].replace(/\s+/g, '') === selectedOption) {
layer.state = !layer.state;
} else {
layer.state = false;
}
}
}
}

// Add an event listener to the dropdown
this.getField("Dropdown2").setAction("Keystroke", "toggleLayers()");


Apologies if this code looks weird. I've resorted to asking ChatGPT for help.

TOPICS
How to , JavaScript , Modern Acrobat , PDF forms

Views

216

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

correct answers 1 Correct answer

Community Expert , May 08, 2024 May 08, 2024

Yeah, I was going to ask you about that... Just use this:

 

var layers = this.getOCGs();
for (var i in layers) {
	layers[i].state = layers[i].name=="Guides and Grids" || (layers[i].name==event.value);
}

Votes

Translate

Translate
Community Expert ,
May 07, 2024 May 07, 2024

Copy link to clipboard

Copied

> I've resorted to asking ChatGPT for help.

That's your main issue. This code contains a lot of errors.

Votes

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 ,
May 07, 2024 May 07, 2024

Copy link to clipboard

Copied

Get rid of all the other code, and use this as the drop-down's custom Validation script:

 

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

Votes

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 Beginner ,
May 08, 2024 May 08, 2024

Copy link to clipboard

Copied

Good grief. I knew the code should be short, but I didn't think it would be quite that short. That's taken care of a large portion of things, but the background layer (Named "Background and Title") disappears when I use this. Any thoughts?

Votes

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 ,
May 08, 2024 May 08, 2024

Copy link to clipboard

Copied

Yeah, I was going to ask you about that... Just use this:

 

var layers = this.getOCGs();
for (var i in layers) {
	layers[i].state = layers[i].name=="Guides and Grids" || (layers[i].name==event.value);
}

Votes

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 Beginner ,
May 08, 2024 May 08, 2024

Copy link to clipboard

Copied

LATEST

It works! Thanks a bunch for the help.

Votes

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
Adobe Employee ,
May 07, 2024 May 07, 2024

Copy link to clipboard

Copied

Hi there

Hope you are doing well and thanks for reaching out.

The workflow you are trying to achieve might be possible using JavaScript. For more information, please check the help pages listed below:
https://acrobatusers.com/tutorials/javascript_console/
https://helpx.adobe.com/acrobat/using/applying-actions-scripts-pdfs.html

Hope it will help

Regards
Amal

Votes

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