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

Assistance with Multiple Checkbox fields showing and hiding same layers

Community Beginner ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

Hi all, hoping for some help here, as I've been working on this for many hours now, but can't seem to find a resolution. I'm fairly new to pdf forms and Javascripting, so don't have much knowledge to pull off from. I am tasked with putting a form together where depending on what checkboxes are selected, certain sections of the form will show and hide layers and form fields. I did figure this out. However, there are a couple checkboxes on the first page that overlap in the sense that they should show the same layers and fields when selected. So if one of these are selected, it should show those fields and layers, if two are selected, they should show, and so on. The problem is, if two or more of those checkboxes are selected, and one of them is deselected, all the layers in the document dissappear. To resolve this, I tried utilizing 'or' statements but can't seem to get it to work. Here is what I have so far. Thank you much for any assistance!

{
var f = this.getField("checkbox 1");
var g = this.getField("checkbox 2");
var h = this.getField("checkbox 3");
if(f || g || h.isBoxChecked(0))
{
if(layer[i].name=="S2")
{layer[i].state=true}
if(layer[i].name=="S22")
{layer[i].state=true}
if(layer[i].name=="S23")
{layer[i].state=true}
if(layer[i].name=="S24")
{layer[i].state=true}
if(layer[i].name=="n4")
{layer[i].state=false}
if(layer[i].name=="n5")
{layer[i].state=false}
if(layer[i].name=="n6")
{layer[i].state=false}
if(layer[i].name=="n7")
{layer[i].state=false}
}
else if(f || g || h.isBoxChecked(1)){
if(layer[i].name=="S2")
{layer[i].state=false}
if(layer[i].name=="S22")
{layer[i].state=false}
if(layer[i].name=="S23")
{layer[i].state=false}
if(layer[i].name=="S24")
{layer[i].state=false}
if(layer[i].name=="n4")
{layer[i].state=true}
if(layer[i].name=="n5")
{layer[i].state=true}
{layer[i].state=false}
if(layer[i].name=="n6")
{layer[i].state=true}
if(layer[i].name=="n7")
{layer[i].state=true}
}
}

 

 

TOPICS
Acrobat SDK and JavaScript

Views

881

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

correct answers 1 Correct answer

Community Beginner , Apr 22, 2022 Apr 22, 2022

Hi all,

 

So I believe this ended up being an easy fix. Of course I missed it in the code, but under the layer n5, I have two layer states specified. {layer[i].state=false} was hiding all the fields. Once I removed it, it toggled the way I wanted it to. Thanks again for your assistance and for taking a look at my issue.

if(layer[i].name=="n4")
{layer[i].state=true}
if(layer[i].name=="n5")
{layer[i].state=true}
{layer[i].state=false}
if(layer[i].name=="n6")
{layer[i].state=true}

 

Votes

Translate

Translate
Community Expert ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

Where does you set i?

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 ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

Ah, sorry, I forgot to mention that. I set it to execute on Mouse up for each checkbox. The first Run a Javascript is showing and hiding the fields, and the second one is showing and hiding the layers using the code above.

 

checkbox properties.JPGexpand image

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 ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

Whoops, I noticed that it was left out while editing the script. This is what should be at the top. Even with this though, it's not working for me. Thanks!

 

var layer = this.getOCGs();
for(var i=0; i<layer.length; i++)
{

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 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

Why did you access isBoxChecked on the h variable, but not on f or g? They are treated as booleans, but in fact are Field objects.

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 ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

It's kind of a Frankenstein at this point, as I referenced alot of other forum posts. I don't really have Javascript experience, but got stuck doing the job. I guess the way I understood it was, first, get the layers, than get the fields for each checkbox, and if the box is checked for f or g or h, then the layers should turn on. If they're not selected, the layers should turn off.

 

I did add isBoxChecked to f and g now, and the layers do toggle on, but don't toggle off when deselecting. Here's what I have now in the script. Thank you for taking a look!

var layer = this.getOCGs();
for(var i=0; i<layer.length; i++)
{
varf = this.getField("Checkbox 1");
varg = this.getField("Checkbox 2");
varh = this.getField("Checkbox 3");
if(f.isBoxChecked(0)||g.isBoxChecked(0)||h.isBoxChecked(0))
{
if(layer[i].name=="S2")
{layer[i].state=true}
if(layer[i].name=="S22")
{layer[i].state=true}
if(layer[i].name=="S23")
{layer[i].state=true}
if(layer[i].name=="S24")
{layer[i].state=true}
if(layer[i].name=="n4")
{layer[i].state=false}
if(layer[i].name=="n5")
{layer[i].state=false}
if(layer[i].name=="n6")
{layer[i].state=false}
if(layer[i].name=="n7")
{layer[i].state=false}
}
else {
if(layer[i].name=="S2")
{layer[i].state=false}
if(layer[i].name=="S22")
{layer[i].state=false}
if(layer[i].name=="S23")
{layer[i].state=false}
if(layer[i].name=="S24")
{layer[i].state=false}
if(layer[i].name=="n4")
{layer[i].state=true}
if(layer[i].name=="n5")
{layer[i].state=true}
{layer[i].state=false}
if(layer[i].name=="n6")
{layer[i].state=true}
if(layer[i].name=="n7")
{layer[i].state=true}
}
}

 

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 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

Can you share the form?

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 20, 2022 Apr 20, 2022

Copy link to clipboard

Copied

- There must be a space after "var".

- Your code only sets the layers as either visible or not visible. It should do both, depending on the situation. One way to do that is to first hide all layers, and then only change the state to true if they should be visible.

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 ,
Apr 20, 2022 Apr 20, 2022

Copy link to clipboard

Copied

Thank you both for your help! I did add that space after var, and the layers are now toggling on and off. However, I'm experiencing an issue with deselecting. When a box is deselected, it hides all other layers, even though they are not specified in the Javascript. I'm not sure if I can share the form directly, but the below is how I have it looking.

 

Thanks for the suggestion try67! Unfortunately, I have two layers for each page. One should always show if that section is not selected. They wanted a visual indicator for blank pages if a section is not selected, so I will have an initial state which shows those layers.

 

Main page of form. The initial state should have page 1 and all n# layers shown. Upon selecting a checkbox, it should show all s# layers and fields associated with that section and turn off n# layers.

Home Page.JPGexpand image

 

If not selected, the n# layers show the following text.

Not selected.JPGexpand image

If selected, then s# layers and fields for that section will show.

selected.JPGexpand image

 

Thank you again for taking a look, and I'd appreciate any further help!

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 ,
Apr 22, 2022 Apr 22, 2022

Copy link to clipboard

Copied

LATEST

Hi all,

 

So I believe this ended up being an easy fix. Of course I missed it in the code, but under the layer n5, I have two layer states specified. {layer[i].state=false} was hiding all the fields. Once I removed it, it toggled the way I wanted it to. Thanks again for your assistance and for taking a look at my issue.

if(layer[i].name=="n4")
{layer[i].state=true}
if(layer[i].name=="n5")
{layer[i].state=true}
{layer[i].state=false}
if(layer[i].name=="n6")
{layer[i].state=true}

 

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