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

Can someone help me with creating a javascript please?

Community Beginner ,
Aug 05, 2019 Aug 05, 2019

Copy link to clipboard

Copied

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

Views

641

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

Just some layers not all.

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

Copy link to clipboard

Copied

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?

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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

        } 

    } 

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

Copy link to clipboard

Copied

No, like I posted it.

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

Copy link to clipboard

Copied

That unfortunately does nothing?

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

Call the function like this:

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

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

Copy link to clipboard

Copied

Screenshot.png

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

Copy link to clipboard

Copied

I'm getting this error code?

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

Copy link to clipboard

Copied

Use the function of reply 8

Don't change the function definition.

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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.

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