Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript, Dropdown lists, and autofill fields

Contributor ,
Mar 14, 2025 Mar 14, 2025

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!

 

 

TOPICS
Create PDFs , How to , JavaScript , PDF , PDF forms
1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 17, 2025 Mar 17, 2025

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2025 Mar 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 17, 2025 Mar 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! 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2025 Mar 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 17, 2025 Mar 17, 2025

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2025 Mar 17, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 17, 2025 Mar 17, 2025

Thank you so much for your help and for the sample!

I'm running into an error on mine that isn't happening with yours so I'm going to try to just copy/paste your fields into my form and see how I get on from there. I'll let you know. 🙂

 

Thank you again so much!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 19, 2025 Mar 19, 2025

This is working so much better for me that what I was doing. Thank you so much for this!

 

Have a terrific day! 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 27, 2025 Mar 27, 2025
LATEST

Hi!

 

Everything worked out tremendously, thank you again so much for your help and the sample (blank) that you attached.

 

I'm wondering if it's possible to autofill more than one field based on a selection from the dropdown.

 

Using your uploaded "Blank.pdf" for my question:

 

When I select an option from "Dropdown1", "Field06" autofills with a dollar amount that it pulls from the "export value" box in Properties > Options.

 

What if I wanted that selection from "Dropdown1" to also autofill "Field07" with a description?

 

Would that throw me back into using special script like I was when I first posted my question or is there an easier way, like what you showed me?

 

Thank you again so much!! The form I was creating, that your sample helped me to do, is in use right now. 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines