Skip to main content
Known Participant
April 12, 2018
Question

Show/hide layer conditional to checkbox and Text Field

  • April 12, 2018
  • 4 replies
  • 4111 views

Hi, I'm currently using that code to show a layer named PA40. If the box is checked, it will show the layer, if it's not, the layer will close.

var layerName = "PA40";

var layers = this.getOCGs(0&&1);

if (layers!=null && layers.length!=0) {

for (var i in layers) {

if (layers.name==layerName) {

layers.state = !layers.state;

break;

}

}

}

I now wish to use that checkbox to make another thing: if the checkbox PA40 and the Text Field named PIED is 18 or higer, it will show the layer named PA4, and if the Text Field named PIED is lower then 18, it will show the layer PA4L.

I'm a total noob in Javascript.

Thanks a lot

This topic has been closed for replies.

4 replies

JR Boulay
Adobe Expert
April 12, 2018

Beware that an OCG state is not saved with the document and revert to default each time the document is closed.

Acrobate du PDF, InDesigner et Photoshopographe
Participating Frequently
March 7, 2023

Why is that when I try this code nothing works.  I need to show or hade a layer I created using a check box. 

JR Boulay
Adobe Expert
April 12, 2018

I now wish to use that checkbox to make another thing: if the checkbox PA40 and the Text Field named PIED is 18 or higer, it will show the layer named PA4, and if the Text Field named PIED is lower then 18, it will show the layer PA4L.

Place this script in the checkbox:

if (event.target.value != "Off") && (this.getField("PIED").value >= 18) {

    var ocgArray1 = this.getOCGs();

    for (var i=0; i < ocgArray1.length; i++) {

        if (ocgArray1.name == "PA4") {ocgArray1.state = true;}

        if (ocgArray1.name == "PA4L") {ocgArray1.state = false;}

    }

}

else if (event.target.value != "Off") && (this.getField("PIED").value < 18) {

    var ocgArray1 = this.getOCGs();

    for (var i=0; i < ocgArray1.length; i++) {

        if (ocgArray1.name == "PA4") {ocgArray1.state = false;}

        if (ocgArray1.name == "PA4L") {ocgArray1.state = true;}

    }

}

else {

    var ocgArray1 = this.getOCGs();

    for (var i=0; i < ocgArray1.length; i++) {

        if (ocgArray1.name == "PA4") {ocgArray1.state = false;

      if (ocgArray1.name == "PA4L") {ocgArray1.state = false;

     }

}

Acrobate du PDF, InDesigner et Photoshopographe
Known Participant
April 13, 2018

I enter the code JR_Boulay and it doesn't work. I put it in a JavaScript tester and there are errors (something about identifier and operators).

try67
Adobe Expert
April 13, 2018

You need to decide, are you using a layer or a field?

Known Participant
April 12, 2018

Thanks, I changed it to this below and it still doesn't work.

var layerName = "PA40";

var layers = this.getOCGs(0).concat(this.getOCGs(1));

if (layers!=null && layers.length!=0) {

for (var i in layers) {

if (layers.name==layerName) {

layers.state = !layers.state;

break;

}

}

}

Been able to make it work by writing the code two times, one with ) and one with 1.

For my questions I was wondering if it would be easier to make a textfield appear instead of a layer, since I only have one item on it.

Thanks a lot

try67
Adobe Expert
April 12, 2018

Yes, fields are much easier to work with than layers.

Known Participant
April 12, 2018

So is it possible to make a field written 123456 if PA40 is checked and PIED is 18 or over?

try67
Adobe Expert
April 12, 2018

First of all, what's this line supposed to be?

var layers = this.getOCGs(0&&1);

If you're trying to get the OCGs from pages 0 and 1 like that, that's not how it's done.

Change it to:

var layers = this.getOCGs(0).concat(this.getOCGs(1));