Skip to main content
Participant
August 18, 2025
Question

If/then dropdown script

  • August 18, 2025
  • 1 reply
  • 328 views

Hi smart people. Need some help with writing a JavaScript if/then for a pdf form. 

if the selection in dropdown11 is "No" than the selection in dropdown12 is "N/A"

 

and 

 

if the selection in dropdown11 is "Yes" than selection in dropdown12 cannot be "N/A"

1 reply

Thom Parker
Community Expert
Community Expert
August 18, 2025

There's a little complexity to this.  One way to do this is to use two scripts, i.e., Validation scripts on both dropdowns. 

 

Validation Script for "dropdown11"

var oFld12 = this.getField("dropdown12");
if(event.value == "No")
   oFld12.value = "N/A";
else if((event.value == "Yes") && (oFld12.value == "N/A"))
   oFld12 = "SomethingElse";

// Note, you'll need to provide a valid value for dropdown12

 

Validation Script for "dropdown12"

event.rc = !((this.getField("dropdown11").value == "Yes")&& (event.value == "N/A"));

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
August 18, 2025

Thanks, that worked; however, if either box is edited after initial selection and breaks the "rule" it accepts the wrong answer. Anyway remedy for that?

Thom Parker
Community Expert
Community Expert
August 18, 2025

Ok, a little more complex that I originally thought.  First, the value of "dropdown11" hasn't changed by the time "dropdown12"  gets the change from the "dropdown11" script.  So there needs to be a blocking mechanism.  And there is a condition missing from the "dropdown12" scripts. 

Finally, both dropdowns need to be set to "commit immediately"

 

Here's the tested code:

 

Validation Script for "dropdown11"

var oFld12 = this.getField("dropdown12");
this.change = true;
if(event.value == "No")
   oFld12.value = "N/A";
else if((event.value == "Yes") && (oFld12.value == "N/A"))
   oFld12.value = "A";
this.change = false;

 

Validation Script for "dropdown12"

if(!this.change)
	event.rc = (this.getField("dropdown11").value == "No")&& (event.value == "N/A") || (this.getField("dropdown11").value == "Yes")&& (event.value != "N/A");

 

Note that the code for dropdown11 uses "A" as the default value for dropdown12. Change this to the default value on your form for dropdown12.

 

 

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