Skip to main content
Participant
June 4, 2025
Question

Auto Populate Text When Drop Down Selection Is Made

  • June 4, 2025
  • 5 replies
  • 728 views

Can someone please my script please, when selecting itme in the drop down nothing is polulated 

 

var f = this.getField("Aircraft");
if(event.value == "Phenom 300")
f.value = "313.26";
else if(event.value == "Challenger 300")
f.value = "636.12";
else if(event.value == "Challenger 350/3500")
f.value = "636.12";
else if(event.value == "Global Express")
f.value = "758.51";

5 replies

Participant
June 4, 2025

Thank you very much 

PDF Automation Station
Community Expert
Community Expert
June 4, 2025

@Thom Parker and @try67 have good advice for you to troubleshoot this.  There's an easier way to write this script by setting the export values of the dropdown to the numbers you want want to populate (example:  display value is "Phenom 300" with export value of 313.26).  If you use a calculation script in the Aircraft field it is:

event.value=this.getField("Your Dropdown Name").value;

If you want the user to to be able to change the value of the Aircraft field after the selection has been made, use the following custom keystroke script in the dropdown:

if(!event.willCommit)
{this.getField("Aircraft").value=event.changeEx}

In both cases, ensure you select Commit selected value immediately in the options tab of the dropdown.

try67
Community Expert
Community Expert
June 4, 2025

Generally I would agree with this approach, but it's problematic to have multiple items with the same export value, though. For starters, the UI "glitches" when you do that and try to select one of those items. Also, if you export the form data later on it will export the export values (as it should), not the actual text, which is always the desired outcome.

PDF Automation Station
Community Expert
Community Expert
June 4, 2025

Thanks for the heads up.  I've never had the UI glitch with duplicate export value.

Participant
June 4, 2025

I have  attached the file, 

The form should change the Arm (Text27), when the aircraft model is selected in the (Aircraft) drop down list.

try67
Community Expert
Community Expert
June 4, 2025

You have it set up backwards... You're editing the value of the drop-down, not the text field.

Change it to:

var f = this.getField("Aircraft");
if (f.value == "Phenom 300")
    event.value = "313.26";
else if (f.value == "Challenger 300")
    event.value = "636.12";
else if (f.value == "Challenger 350/3500")
    event.value = "636.12";
else if (f.value == "Global Express")
    event.value = "758.51";
else event.value = "";

I would also recommend ticking the box to commit the selected value immediately, under the Properties dialog of the drop-down field, in the Options tab.

try67
Community Expert
Community Expert
June 4, 2025

Code seems fine. Where did you place it (under which event of which field)?

Did you check the JS Console for errors?

I would also recommend adding a final "else" clause to set the field as empty, or zero, or show an error message.

Thom Parker
Community Expert
Community Expert
June 4, 2025

On the surface there is nothing wrong with the script, but we're lacking details and there are many places where it could go bad.

Look in the console window for any reported errors.  (Ctrl-j)

And please provide more details.

Is this a "Validation" script on the dropdown?  

Does the text in the dropdown exactly match the text used in the script?

Add this line of code to the top of your script.

 

     console.println("Selected:" + event.value);

 

Then open the console window (ctrl-j) and make some selections. What text is printed in the console?

 

 

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