Copy link to clipboard
Copied
Hi,
I'm trying to setup a button to turn off 9 layers on an interactive pdf form.
8 out of 9 of the layers turn off no problem but the 1 layer remains turned on. Really not sure why this is happening. Double checked obvious things like layer names but to no avail. The WR1 layer will not turn off when I click the reset button.
Could anyone please help me with this 'simple' task as I am not particularly advanced with Javascript.
PDF form attached here https://we.tl/t-gS6R4unxWl
Thanks in advance.
var layer = this.getOCGs();
for(var i=0; i<layer.length; i++)
{
if(event.target.value=="Yes")
{
if(layer.name=="WR1")
{layer.state=false};
}
{
if(layer.name=="CL1")
{layer.state=false};
}
{
if(layer.name=="NC1")
{layer.state=false};
}
{
if(layer.name=="WR2")
{layer.state=false};
}
{
if(layer.name=="CL2")
{layer.state=false};
}
{
if(layer.name=="NC2")
{layer.state=false};
}
{
if(layer.name=="WR3")
{layer.state=false};
}
{
if(layer.name=="CL3")
{layer.state=false};
}
{
if(layer.name=="NC3")
{layer.state=false};
}
}
Message was edited by: Ross Jackson
That's because this field is not a check-box, but a button, so it doesn't have any value.
Remove the first and last lines of the code and try again.
Copy link to clipboard
Copied
Where is the reset button?
Copy link to clipboard
Copied
Ideally I would like the layers to toggle on/off with the 3 small buttons above each logo but also act like radio buttons so you can only choose one at a time.
Thanks for your help.
Copy link to clipboard
Copied
Your code is not set up correctly. This line only applies to the first layer:
if (event.target.value=="Yes")
I recommend you use indentation to figure out what belongs with what.
I would also move that line to be the first one of the code, before the for-loop.
Copy link to clipboard
Copied
This is a more compact and easier to follow version of the code above:
if (event.target.value=="Yes") {
var layers = this.getOCGs();
if (layers!=null) {
for (var i=0; i<layers.length; i++) {
if (layers.name=="WR1") layers.state=false;
if (layers.name=="CL1") layers.state=false;
if (layers.name=="NC1") layers.state=false;
if (layers.name=="WR2") layers.state=false;
if (layers.name=="CL2") layers.state=false;
if (layers.name=="NC2") layers.state=false;
if (layers.name=="WR3") layers.state=false;
if (layers.name=="CL3") layers.state=false;
if (layers.name=="NC3") layers.state=false;
}
}
}
Copy link to clipboard
Copied
Thanks for this revision. Unfortunately it doesn't seem to be working for me now. The layers are staying switched on when the reset button is clicked.
Copy link to clipboard
Copied
That's because this field is not a check-box, but a button, so it doesn't have any value.
Remove the first and last lines of the code and try again.
Copy link to clipboard
Copied
Thank you so much. That's exactly what I wanted it to do!
Have a great day!