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

Can someone help me with creating a javascript please?

Community Beginner ,
Aug 05, 2019 Aug 05, 2019

i Have a javascript that can turn the layers i want on. but what i would love to include is script that will turn some other layers off once the layer is toggled on. Here is what i have so far.

var docOCGs = this.getOCGs();

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

{

          if(docOCGs.name == "Cadent Trace" ||

     docOCGs.name == "Cadent Trace")

          {

                    docOCGs.state = !docOCGs.state;

          }

}

I am very new to java and am learning the basics at the moment so any help will be much appreciated!

thanks, Max

TOPICS
Acrobat SDK and JavaScript
1.3K
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 ,
Aug 05, 2019 Aug 05, 2019

Try to describe in words exactly what your script is supposed to do - e.g. something like "I have three layers 'layerA', 'layerB' and 'layerC', and when XYZ happens, then layer A and layerB should be shown, layerC should be hidden. When ABC happens, then layerA should be hidden and layerB and layerC should be visible.

Based on your question it's not clear what you want the script to do.

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 ,
Aug 05, 2019 Aug 05, 2019

Sorry ill try to explain better. If 'Layer 1' is on then 'Layer 2', 'Layer 3' & 'Layer 4' should be off. I want to be able to switch between these for example if 'layer 2' is visible then 'layer 1', 'layer 3' & ' layer 4' should not be visible and so on. I hope this explains it a bit better.

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 ,
Aug 05, 2019 Aug 05, 2019

Do you want to hide 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 ,
Aug 05, 2019 Aug 05, 2019

Just some layers not all.

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 ,
Aug 05, 2019 Aug 05, 2019

OK, but your code doesn't just set some layers as visible. It changes their state from visible to hidden, or the other way around.

So should these other layers also be changed to the opposite state from their current one?

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 ,
Aug 05, 2019 Aug 05, 2019

Yeah so the selected check box will make the selected layer visible and the rest hidden. so for each layer there is a checkbox that should make the layer visible but also hide the rest.

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 ,
Aug 05, 2019 Aug 05, 2019

OK, that's a bit more clear. You can use this code as a doc-level script:

function showOneLayer(allLayers, layerToShow) {

    var docOCGs = this.getOCGs();

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

        var layer = docOCGs;

        if (allLayers.indexOf(layer.name)!=-1) {

            layer.state = (layerName==layerToShow);

        }

    }

}

And then call it from your check-boxes like this (to make the first layer visible, for example):

showOneLayer(["Layer1", "Layer2", "Layer3", "Layer4"], "Layer1");

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 ,
Aug 05, 2019 Aug 05, 2019

Like this?

function showOneLayer(["ESP Scan", "SP Scan", "BT Scan", "GTC Scan"], "Cadent Trace"); { 

    var docOCGs = this.getOCGs(); 

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

        var layer = docOCGs

        if (allLayers.indexOf(layer.name)!=-1) { 

            layer.state = (layerName==layerToShow); 

        } 

    } 

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 ,
Aug 05, 2019 Aug 05, 2019

No, like I posted it.

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 ,
Aug 05, 2019 Aug 05, 2019

That unfortunately does nothing?

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 ,
Aug 05, 2019 Aug 05, 2019

You also have two call the function when your checkboxes get changed (or whatever the trigger is).

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 ,
Aug 05, 2019 Aug 05, 2019

How do i do that? i'm really finding javascript difficult to understand so i need it to be explained in 'dumb' terms

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 ,
Aug 05, 2019 Aug 05, 2019

Do you not need to specify the layer name, can you just call it 'layer 1'?

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 ,
Aug 05, 2019 Aug 05, 2019

Call the function like this:

showOneLayer(["ESP Scan", "SP Scan", "BT Scan", "GTC Scan"], "Cadent Trace");

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 ,
Aug 05, 2019 Aug 05, 2019

Screenshot.png

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 ,
Aug 05, 2019 Aug 05, 2019

I'm getting this error code?

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 ,
Aug 05, 2019 Aug 05, 2019

Use the function of reply 8

Don't change the function definition.

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 ,
Aug 05, 2019 Aug 05, 2019
LATEST

Let's take a step back. The right way to do this is to put this project on the back burner and learn a bit more about JavaScript first. If you want to start with a free book, take a look here: Learning to Program JavaScript for Adobe Acrobat - KHKonsulting LLC

Look especially into how JavaScript functions work. There are always two parts to calling a function: You need to define the function and then you need to call that function. To call a function with parameters, you need to know how the function interprets these parameters (meaning what type of parameter the function expects). Here is a quick example. Let's first define a function that takes one parameter - a simple string:

function sampleFunction(myString) {

    // now we are doing something with the parameter that was passed into this function:

    console.println("The parameter is = " + myString);

}

That by itself will not do anything until you call the function:

   // some other code goes here

  sampleFunction("This is my string parameter");

  // some more code

In your screen shot, you are mixing these two steps into one. It is important that you know what these two different steps look like - and where they go. As was mentioned earlier, the function should be defined in a document level script (it does not have to, but that's usually the best location to put functions). You then call the function from the mouse up actions of your checkboxes.

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 ,
Aug 05, 2019 Aug 05, 2019

The name of the layer to show has to be included in the list of all layers, though.

It can be adjusted so that's not the case, and the list is only of layers to hide, of course.

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 ,
Aug 05, 2019 Aug 05, 2019

PS. This part of your code doesn't make sense:

docOCGs.name == "Cadent Trace" || docOCGs.name == "Cadent Trace"

You're basically saying:

IF (X IS Y) OR (X IS Y)

PPS. The name of this language is JavaScript, not Java. Similar names but quite different languages.

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