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

Set value of a textbox if a dropdown value has certain value

Community Beginner ,
Oct 24, 2024 Oct 24, 2024

Using Acrobat Pro, I have a dropdown field called Program. User will select from three options

  1. Choose One (this is default to prompt user to select something)
  2. SNAP
  3. TANF

 

I then have Textbox field that is called programLong. I want this field to auto update to the long names for each or be blank if option 1 is not changed.

Here is my code that will only update the programLong filed if I preview and then click Edit. It will not update when the dropdown field changes values.

 

var progValue = this.getField("Program").value;
if (progValue == "SNAP") {
event.value = "Supplemental Nutrition Assistance Program (SNAP)";
}
else if (progValue == "TANF"){
event.value = "Temporary Assistance for Needy Families (TANF)";
}
else if (progValue == "Choose One"){
event.value ="";
}

TOPICS
Edit and convert PDFs , How to , JavaScript , PDF
1.2K
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 ,
Oct 24, 2024 Oct 24, 2024

+++EDITED REPLY... I made a mistake

 

Hi there @David38818339zr2v ,

 

Here is a working example using Acrobat's built-in features and one line of code employed as a Custom Calculating Script: https://acrobat.adobe.com/id/urn:aaid:sc:US:df02540b-5841-436a-963f-8d0cad6a5eab

 

In this particular example, I am assuming that the users will not be able to type in any data in the "programLong field. In which case, you can keep that textfield as readonly. Therefore,  I am assigning to each item of the dropdown menu the long answer as the export value (illustrated in my screenshot below).

 

The Custom Calculating Script Script that I am using on "programLong" textfield :

 

 

var progValue = this.getField("Program").value;
(progValue == "Choose One")? event.value =="" : event.value = progValue

 

 

 

The script will execute as soon as the selection is commited in the dropdown field "Program", populating the "programLong"  textfield with the desired long answer (export value).

 

See screenshot:

 

snap.png

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 Beginner ,
Oct 24, 2024 Oct 24, 2024

I have tried to place this script on a the calculated field on Program and programLong fields with no success.

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 ,
Oct 24, 2024 Oct 24, 2024

+++EDITED REPLY... I made a mistake

 

Hi there @David38818339zr2v ,

 

Here is a working example using Acrobat's built-in features and one line of code employed as a Custom Calculating Script: https://acrobat.adobe.com/id/urn:aaid:sc:US:df02540b-5841-436a-963f-8d0cad6a5eab

 

In this particular example, I am assuming that the users will not be able to type in any data in the "programLong field. In which case, you can keep that textfield as readonly. Therefore,  I am assigning to each item of the dropdown menu the long answer as the export value (illustrated in my screenshot below).

 

The Custom Calculating Script Script that I am using on "programLong" textfield :

 

 

var progValue = this.getField("Program").value;
(progValue == "Choose One")? event.value =="" : event.value = progValue

 

 

 

The script will execute as soon as the selection is commited in the dropdown field "Program", populating the "programLong"  textfield with the desired long answer (export value).

 

See screenshot:

 

snap.png

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 Beginner ,
Oct 24, 2024 Oct 24, 2024

Nice and simply, I love it! Better than what I was trying to use.

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 ,
Oct 24, 2024 Oct 24, 2024

It won't work in the Program field because you are using event.value.  It should work in the programLong field as a custom calculation script.  If you are going to use it in the Program field it would be a validation script, but you have to change this.getField("Program").value to event.value and change all event.value's to this.getField("programLong").value.  Also select "Commit selected value immediately" in the options tab of the dropdown.  These two articles explain calculation vs validation scripts:

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

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

There is an easier way though:

1)  Set the corresponding export values of "SNAP" and "TANF" to the long versions.

2)  Enter the following custom calculation script in the programLong field:

event.value=(this.getField("Program").value=="Choose One")?"":this.getField("Program").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
Community Beginner ,
Oct 24, 2024 Oct 24, 2024

Nice and simply, I love it! Better than what I was trying to use.

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 ,
Oct 24, 2024 Oct 24, 2024

You can also use:

event.value=this.getField("Program").value;

But you have to set the export value of Choose One to null and that can only be done with a script or a tool like this one I developed (paid for).

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 Beginner ,
Oct 25, 2024 Oct 25, 2024
LATEST

There is another way, one I am not fond of, but I used a single space. Not one that I would normally encourage, but it works for our purpose.

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