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

JavaScript Help

Community Beginner ,
Jul 22, 2019 Jul 22, 2019

Hi there, Could someone help me correctly write a javascript. What i want to do is get the layers to switch between themselves when another layer is toggled on. So when toggling through the layers they don't all stay on but rather switch between them.

This is the script I have so far,

var docOCGs = this.getOCGs();

for (var x=0; x < docOCGs.length; x++)

{

          if(docOCGs.name == "Layer 1" ||

     docOCGs.name == "Layer 1")

          {

                    docOCGs.state = !docOCGs.state;

          }

}

Thank you.

TOPICS
Acrobat SDK and JavaScript
816
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 ,
Jul 22, 2019 Jul 22, 2019

Hi,

I have not been able to test this, but could you not do something like.

var docOCGs = this.getOCGs();

for (var x=0; x < docOCGs.length; x++)

{

     // make then all hidden

     docOCGs.state =false;

     if (docOCGs.name == "LayerNameWeAreLookingFor") {

          docOCGs.state = true;

     }

}

Hope this helps

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 ,
Jul 22, 2019 Jul 22, 2019

This should work, but you can improve and simplify the code a bit by replacing these lines:

     docOCGs.state =false;

     if (docOCGs.name == "LayerNameWeAreLookingFor") {

          docOCGs.state = true;

     }

With just:

docOCGs.state = (docOCGs.name == "LayerNameWeAreLookingFor");

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 Beginner ,
Jul 22, 2019 Jul 22, 2019

Thank you but this code makes everything disappear including my template layer. It also doesn't toggle the layer off when i click on the next box. Am i doing something wrong?

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 ,
Jul 22, 2019 Jul 22, 2019

Did you adjust the name of the layer you want to show inside the quotes?

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 Beginner ,
Jul 22, 2019 Jul 22, 2019

Yeah i changed it to the layer i want to be toggled on and off when selecting the next layer box

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 ,
Jul 22, 2019 Jul 22, 2019

Do you want to toggle ​all​ other layers, or just some?

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 Beginner ,
Jul 22, 2019 Jul 22, 2019

Just some please

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 ,
Jul 22, 2019 Jul 22, 2019

That requires a more complex script. You'll need to define a list of layers to show/hide, or of ones to ignore, and use that for in the loop.

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 Beginner ,
Jul 24, 2019 Jul 24, 2019

I have no experience with Javascripts as I'm sure you can tell. Could you please help me with that?

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 ,
Jul 24, 2019 Jul 24, 2019

I hope this does not come as a surprise to you, but you cannot program in JavaScript without knowing about the language. The good news is that it's not too complex to learn the basics, where it then gets more complex is to learn about the application specific functions (like e.g. layers in Acrobat). You also cannot copy and paste your way to a working solution, so do yourself a favor and start to learn JavaScript. You can find out how to do that here: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC

If this is the only JavaScript project you think you'll be working on for a long time, it may be easier and cheaper to just pay somebody to do this job for you. 

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 Beginner ,
Jul 24, 2019 Jul 24, 2019
LATEST

I am starting to learn Javascript with some online courses and videos as i will be using javascript more frequently in the future as this is my first project of many to come. Thank you for the link its much appreciated!

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