4 Replies Latest reply on Nov 9, 2017 1:10 PM by Thom Parker

    Using Javascript Action in Dropdown to hide/show a layer

    chanceb1138722 Level 1

      I have a PDF set up with a dropdown. The dropdown named "Country" allows users to select their Country/Region. I have 27 other layers in my PDF, each having an image of a flag. They are stacked on top one another. My hopes is to have the layer toggle from hidden to visible based on the selected country in the dropdown. I found many topics about buttons or checkboxes, but could not find anything based on dropdowns.

       

      Please find my PDF through the link below

       

      Shared Files - Acrobat.com

        • 1. Re: Using Javascript Action in Dropdown to hide/show a layer
          try67 MVP & Adobe Community Professional

          Use the code you found. You can use the drop-down's validation script, and access its new value using event.value.

          Make sure to tick the option to commit the selected value immediately, for it to work smoothly. You'll find it under the field's Properties, Options tab.

          • 2. Re: Using Javascript Action in Dropdown to hide/show a layer
            Thom Parker Adobe Community Professional

            Your best option is any of the field scripts that trigger when an option is selected from the drop down. My favorite is the Keystroke script on the "Format" tab, when the "Custom" option is selected. 

             

            For scripting purposes it is best to have a direct relationship between the options in the dropdown and the OCGs (Layers). I noticed that the OCGs on the PDF are named "Layer 1", etc. These names make the scripting more complicated, because you need to somehow relate "Layer 2" to "USA".

             

            For example, have the OCGs named for the country, so the OCG name and the dropdown name are the same, i.e. the layer for USA is named "USA". Or have them in the same order so the index if the dropdown selection is the same as the position of the the OCG in the returned array. I don't like this one cause it's hard to manage.  Here's an example that assumes the names are the same.

             

            In Format Keystroke script

            if(event.willCommit)

              this.getOCGs().some(function(a){return a.state = (a.name==event.value);});

             

            To use the code you'll need to do two things,

            1. Rename all layers to their country names

            2. Flatten out "Layer 1", since it is the main background for the document.

            • 3. Re: Using Javascript Action in Dropdown to hide/show a layer
              chanceb1138722 Level 1

              Thank you Thom. Great explanation and easy to follow. Simple to set up and it all worked out nicely. There were times where if you clicked on one Country and then clicked on another without going to another field both layers would be visible but I can just prompt people to click on another field first if they need to chose another Country from the dropdown.

              • 4. Re: Using Javascript Action in Dropdown to hide/show a layer
                Thom Parker Adobe Community Professional

                Thank you. I'm glad it worked out so well.  To solve the issue with having to click on another field, set the "Commit Selected Value Immediately" check box on the "Options" tab of the "Properties" dialog for the dropdown.