Skip to main content
Known Participant
February 18, 2025
Answered

Java script to show/hide layers and checkboxes

  • February 18, 2025
  • 2 replies
  • 2986 views

I'm trying to control the visibility of certain checkboxes and layers on my PDF form according to what is selected in a drop down menu. I've found 2 scripts; one to show/hide the layers and one to show/hide the checkboxes. I'm a novice when it comes to java scripting. How do I combine them?

 

If "Option1" is selected from the dropdown, the layer of the same name along with Box3 and Box4 should show.

If "Option2" is selected, the layer of the same name along with Box1 and Box2 should show.

If "-Select-" is selected (default), none of the options above should show.

 

Layers script:

var layers = this.getOCGs();

var v = event.value;

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

    if (layers[i].name == “Option1” && v == "Option1") {

        layers[i].state = true;

    }

    else if (layers[i].name == “Option2” && v == "Option2") {

        layers[i].state = true;

    }

    if (layers[i].name == "Option1" && v == "Option2") {

        layers[i].state = false;

    }

    else if (layers[i].name == "Option2" && v == "Option1") {

        layers[i].state = false;

    }

    if (layers[i].name == "Option1" && v == "-Select-") {

        layers[i].state = false;

    }

    if (layers[i].name == “Option2” && v == "-Select-") {

        layers[i].state = false;

    }

}

 

Checkbox script:

var v = event.value;

if(v == "-Select-"){

this.getField(“Box1”).display = display.hidden;

this.getField("Box2").display = display.hidden;

this.getField("Box3”).display = display.hidden;

this.getField("Box4").display = display.hidden;}

if(v == "Option2"){

this.getField("Box1").display = display.visible;

this.getField("Box2").display = display.visible;

this.getField("Box3”).display = display.hidden;

this.getField("Box4").display = display.hidden;}

if(v == "Option1"){

this.getField("Box1").display = display.hidden;

this.getField("Box2").display = display.hidden;

this.getField("Box3”).display = display.visible;

this.getField("Box4").display = display.visible;}

 

Any help or advice would be greatly appreciated!

Thanks,

Trisha

Correct answer PDF Automation Station

Sorry, you're right. It's just not what I was expecting. As I've said, I'm a newbie at complicated forms and java.

 

When the facility is selected in Reader, it's doesn't make the layers and items for that facility visible until the document is saved and reopened. Is there a way to change that?


It works in Reader but you have errors in the calculation script when used with Reader.  layers[i].initState can't be used in Reader.  Remove all instances and it should work.

2 replies

try67
Community Expert
Community Expert
February 18, 2025

Just put them one after another. They should work independently of each other.

Known Participant
February 19, 2025

That's what I thought but it didn't work at all when I combined them.

PDF Automation Station
Community Expert
Community Expert
February 19, 2025

Which part doesn't work?  Do both scripts work independently?  If so they should work together.  If not, check the console for errors.

PDF Automation Station
Community Expert
Community Expert
February 18, 2025

Removed by author.

Known Participant
February 19, 2025

Not quite what I'm trying to do. The link you provided is about using a checkbox to control layer visibility.

I'm want to use a drop down to control the visibility of both layers and checkboxes.