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

Show/hide layer conditional to checkbox and Text Field

New Here ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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

TOPICS
PDF forms

Views

2.6K

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 ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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

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
New Here ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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

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 ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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

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
New Here ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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

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 ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

Sure. What should it be otherwise, though?

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
New Here ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

Sorry I might not be clear enough (or my English is terrible). I'm working on a PDF that will make a drawing of a pole and a certain piece number has to be different depending on the height of the pole.

If I check the box PA40, and the number in the field PIED is lower then 18 (event.value < 18), a text field written 123456 will appear.

If I check the box PA40, and the number in the field PIED is 18 or higher (event.value >= 18), a text field written 789456 will appear.

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 ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

My question was: What should be the value of this text field when PA40 is not checked?

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
New Here ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

If the box is not checked the box will be empty or not visible. They have to check the box and enter a dimension.

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 ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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;

     }

}

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
New Here ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

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

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 ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

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

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
New Here ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

I think a field would be easier to handle since I already have like 20 layers (and would need to create like 10 more).

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 ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

OK, I think that's a wish choice. You can use this code as the custom calculation script of your text field, then:

var v1 = this.getField("PA40").valueAsString;

var v2 = Number(this.getField("PIED").valueAsString);

if (v1=="Off") event.value = "";

else {

    if (v2<18) event.value = "123456";

    else event.value = "789456";

}

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
New Here ,
Apr 13, 2018 Apr 13, 2018

Copy link to clipboard

Copied

This works perfectly!! Thanks a lot guys, you deserve a box of cookies!!

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 ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

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

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
New Here ,
Mar 07, 2023 Mar 07, 2023

Copy link to clipboard

Copied

LATEST

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

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