Skip to main content
Inspiring
March 14, 2025
Answered

Javascript, Dropdown lists, and autofill fields

  • March 14, 2025
  • 1 reply
  • 1595 views

Hi!

 

Thank you so much to everyone for being here with answers to my many questions! I have a new one.

 

I’m using Adobe Acrobat Standard on Windows 10. 😊

 

I have a PDF with the following fields:

 

Dropdown5 (dropdown list)
FIELD06 (number, no decimals)
FIELD07 (number, no decimals)
FIELD08 (number, two decimals)
FIELD09 (number, two decimals)

 

1. I need “FIELD06” to autofill with a number based on the selection made in “Dropdown5”.

 

The properties for “Dropdown5” are set as follows:

 

Options Tab:

 

Technician/Mechanic
Engineer
Programmer
Travel

Commit selected value immediately (checked)

 

Actions Tab:

 

Select Trigger: On Blur

Select Action: Run a Javascript

 

The javascript entered after clicking “Add” is:

 

var selection = this.getField("Dropdown5").value;
var FIELD06 = this.getField("FIELD06");

switch (selection) {
case "Technician/Mechanic":
FIELD06.value = "138.50";
break;
case "Engineer":
case "Programmer":
FIELD06.value = "250.00";
break;
case "Travel":
FIELD06.value = "110.00";
break;
default:
FIELD06.value = "";
}

 

Format: None

 

2. I then need “FIELD07” to multiply with “FIELD08” and then the result of that to multiply with “FIELD06” and then for that result to autofill into “FIELD09”.

 

The properties for “FIELD06” are set as follows:

 

Format: Number

Decimals: 2

 

The properties for “FIELD07” and “FIELD08” are set as follows:

 

Format: Number

Decimals: 0

 

The properties for “FIELD09” are set as follows:

 

Format: Number

Decimals: 2

 

The Custom Calculation Script is:

 

var FIELD07 = this.getField("FIELD07").value;
var FIELD08 = this.getField("FIELD08").value;
var FIELD06 = this.getField("FIELD06").value;
var FIELD09 = this.getField("FIELD09");

// Ensure values are treated as numbers
FIELD07 = parseFloat(FIELD07) || 0;
FIELD08 = parseFloat(FIELD08) || 0;
FIELD06 = parseFloat(FIELD06) || 0;

// Perform the calculation
FIELD09.value = (FIELD07 * FIELD08) * FIELD06;

 

3. When a selection in “Dropdown5” is changed, I need:

 

A) “FIELD06” to instantly update whenever the selection in “Dropdown5” is changed

which would then

 

B) automatically update the calculation result in “FIELD09”

 

My trouble:

 

When I initially select an option from “Dropdown5”, “FIELD06” does not autofill with any amount, it remains blank until I tab or click to the next field, at which point the correct amount then autofills into “FIELD06”.

 

- and -

 

When I change the selection in the “Dropdown5” field, “FIELD06” does not instantly update to the correct amount – it waits until I tab or click to the next field.

 

My questions:

 

What do I need to do to ensure that “FIELD06” automatically fills with the correct amount when a selection is made in “Dropdown5”, rather than waiting until the user tabs or clicks to the next field? Is it possible?

 

Thank you so much!

 

 

Correct answer PDF Automation Station

Thank you again!

 

Yikes, lol, I am rapidly nearing the end of my being able to do and understand this.

 

Am I able to use this latest script, as it's written, in the Properties > Validate > Run Custom Validation Script section or would I need to make changes to it?

 

Thank you again!


I honestly don't know because I wouldn't write it like that.  Attached is my test file with scripts and formatting as recommended.

1 reply

PDF Automation Station
Community Expert
Community Expert
March 14, 2025

Your script in the dropdown should not be an 'on blur' event.  On blur means it runs when the focus leaves that field (exactly what you are describing by tabbing out of the field).  Use a validation script instead and change this.getField("Dropdown5").value to event.value

Inspiring
March 17, 2025

Thank you!

 

I tried doing that but it didn't work (no doubt something I did wrong).

 

The script has been through a few changes due to additional needs and I've got it doing what I need now.

 

In the field Dropdown5, the script below is entered into Properties > Format > Custom > Custom Format Script:

 

var FIELD06 = this.getField("FIELD06");

switch (event.value.trim()) {  // .trim() removes accidental spaces
    case "Technician/Mechanic":
        FIELD06.value = 138.50;  // Numeric value with 2 decimal places
        FIELD06.readonly = true;
        break;
    case "Engineer":
    case "Programmer":
        FIELD06.value = 250.00;  // Numeric value with 2 decimal places
        FIELD06.readonly = true;
        break;
    case "Engineer Travel":
    case "Programmer Travel":
        FIELD06.value = 110.00;  // Numeric value with 2 decimal places
        FIELD06.readonly = true;
        break;
    case "Tech/Mech Travel":
        FIELD06.value = 82.50;  // Numeric value with 2 decimal places
        FIELD06.readonly = true;
        break;
    case "":
    case " ":
        FIELD06.value = "";  // Clears FIELD06 when no selection is made
        FIELD06.readonly = false;  // Allow input if cleared
        break;
    default:
        FIELD06.value = ""; // Placeholder for custom input
        FIELD06.readonly = false;  // Allow custom input when custom text is entered
}

 

Am I good to go with this or do you foresee any troubles a user might run into?

 

Thank you again so much! 🙂

PDF Automation Station
Community Expert
Community Expert
March 17, 2025

Do not use a format script.  That's not what it's  for and it won't necessarily run when you want it to.  Also, the .trim() function does not work in older versions of Reader and Acrobat.  I would suggest formatting FIELD06 as a number with 2 decimals and enter the following custom calulation script in it:

if(!event.value || event.value==0)
{
event.value="";
event.target.readonly=false;
}
else
{
event.target.readonly=true;
}

 

Next,  make the export values of the dropdown display values the corresponding numbers, and enter the following custom keystroke script in the dropdown:

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

 

For your reference:

https://pdfautomationstation.substack.com/p/calculation-vs-validation-scripts

https://pdfautomationstation.substack.com/p/calculation-vs-validation-scripts-eb5

https://pdfautomationstation.substack.com/p/another-method-for-calculation-vs