Skip to main content
Participating Frequently
February 8, 2024
Answered

PDF Dropdown JavaScript Show/Hide Help

  • February 8, 2024
  • 1 reply
  • 1395 views

Can anyone take a look at my PDF & script to tell me how to get the hidden addresses to appear when an option from the dropdown is selected.

 

Test file attached. Right now, you have to click on a selection from the dropdown, THEN click again anywhere on the PDF for that selection to appear.

 

Script I'm using:

 

var display = { visible: display.visible, hidden: display.hidden }; 

 

function showAddress() {

     var locationValue = this.getField("Location").value; 

 

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

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

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

 

// Show the corresponding display field based on the selected location

if (locationValue === "City, ST 1") {

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

} else if (locationValue === "City, ST 2") {

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

} else if (locationValue === "City, ST 3") {

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

}

}

 

this.getField("Location").setAction("OnBlur", "showAddress()");

This topic has been closed for replies.
Correct answer Thom Parker

You're script is "over-written", i.e., most of it is unnecessary, and improperly placed. 

Put this in the Validation Script for the dropdown.

this.getField("Address1").display = display.hidden;
this.getField("Address2").display = display.hidden;
this.getField("Address3").display = display.hidden;
 
// Show the corresponding display field based on the selected location
if (event.value == "City, ST 1") {
     this.getField("Address1").display = display.visible;
} else if (event.value == "City, ST 2") {
     this.getField("Address2").display = display.visible;
} else if (event.value == "City, ST 3") {
     this.getField("Address3").display = display.visible;
}

 

That's all you need, plus set the "commit seleted immediately" option for the dropdown field.

 

Read more about this topic here:

https://www.pdfscripting.com/public/Hiding-and-Showing-Form-Fields.cfm

https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm

 

 

 

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
February 9, 2024

You're script is "over-written", i.e., most of it is unnecessary, and improperly placed. 

Put this in the Validation Script for the dropdown.

this.getField("Address1").display = display.hidden;
this.getField("Address2").display = display.hidden;
this.getField("Address3").display = display.hidden;
 
// Show the corresponding display field based on the selected location
if (event.value == "City, ST 1") {
     this.getField("Address1").display = display.visible;
} else if (event.value == "City, ST 2") {
     this.getField("Address2").display = display.visible;
} else if (event.value == "City, ST 3") {
     this.getField("Address3").display = display.visible;
}

 

That's all you need, plus set the "commit seleted immediately" option for the dropdown field.

 

Read more about this topic here:

https://www.pdfscripting.com/public/Hiding-and-Showing-Form-Fields.cfm

https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
February 9, 2024

How would I set the "commit selected immediately" in here. Sorry, this is new to me.

 

Thanks for the help so far! 

Thom Parker
Community Expert
Community Expert
February 9, 2024

You are Welcome!

Here's an article on editing field properties. 

https://www.pdfscripting.com/public/Editing-Fields-Properties.cfm

 

Look on the "Options" tab on the Field Properties Dialog for the dropdown. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often