Skip to main content
Participant
Jun 4, 2025 3:08 PM UTC
Question

Auto Populate Text When Drop Down Selection Is Made

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";

10 replies

Participant
Jun 4, 2025 5:01 PM UTC

Thank you very much 

Translate
PDF Automation Station
Community Expert
Community Expert
Jun 4, 2025 4:15 PM UTC

@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.

Translate
try67
Community Expert
Community Expert
Jun 4, 2025 4:24 PM UTC

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.

Translate
PDF Automation Station
Community Expert
Community Expert
Jun 4, 2025 4:34 PM UTC

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

Translate
Participant
Jun 4, 2025 4:05 PM UTC

I have  attached the file, 

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

Translate
try67
Community Expert
Community Expert
Jun 4, 2025 4:17 PM UTC

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.

Translate
try67
Community Expert
Community Expert
Jun 4, 2025 3:43 PM UTC

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.

Translate
Thom Parker
Community Expert
Community Expert
Jun 4, 2025 3:42 PM UTC

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?

 

 

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