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

Auto-populate text from one form field to another depending on which checkbox is ticked

Explorer ,
May 29, 2025 May 29, 2025

I know that we can auto-populate information from one text field to another by keeping the same form name. However, I require a text field to populate in another text field if a checkbox is selected.

 

For example, I have two checkboxes:
__ CURRENT and __ NEW

Amount $________________.

Current Amount $ _____________ (auto-populate from amount field if current is checked)

New Amount $ _______________ (auto-populate from amount field if new is checked)

 

If current is selected, there is an amount field that is to be populated in another text field. If new is selected, that same amount field will be populated in another text field.

 

Is this possible?

 

TOPICS
Create PDFs , How to , JavaScript , PDF , PDF forms , Standards and accessibility
563
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
2 ACCEPTED SOLUTIONS
Community Expert ,
May 29, 2025 May 29, 2025

I wrote the script based on the idea that the checkboxes were named differently. However, it can be easily changed to work if they are named the same, using different export values. 

 

Assuming the same export values specified by @PDF Automation Station and a checkbox name of "check".

NOTE: the field names and export values used in the script must be verbatim those used on the form fields. Capitals count. 

 

// Script for Current Amount field
event.value = (this.getField("Check").value == "CURRENT")?this.getField("Amount").value:"";

 

// Script for New Amount field
event.value = (this.getField("Check").value == "NEW")?this.getField("Amount").value:"";

 

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

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 ,
Jun 02, 2025 Jun 02, 2025

The calculation script is overwritting anything that the user entered.  There are a few different ways to allow the user to enter a value into the field that is not selected.  Probably the easiest is to simple not overwrite the value in the non-selected field. This presents another problem though.  If the user selectes New, then the new feild is populated with the entered value. If they then select Current, then that field is populated with the entered value, but because the non-selected value is not overwritten the New value remains, so both fields now have the same value. 

 

Here are the modified scripts. If this does not work for you then you will need to outline the exact behavior you want. And I mean exact, down to the tiny details. 

 

 

// Script for Current Amount field
if(this.getField("Check").value == "CURRENT")
    event.value = this.getField("Amount").value;

 

// Script for New Amount field
if(this.getField("Check").value == "NEW")
    event.value = this.getField("Amount").value;

 

 

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

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 ,
May 29, 2025 May 29, 2025

You'll need to place a custom calculation script on each of the "Current" and  "New Fields".

 

To do this correctly you'll need to know the exact names of the form fields. The script below uses names I made up. These have to be changed to the names in the real form for the script to work. 

 

// Script for Current Amount field
event.value = (this.getField("CurrentCheck").value != "Off")?this.getField("Amount").value:"";

 

// Script for New Amount field
event.value = (this.getField("NewCheck").value != "Off")?this.getField("Amount").value:"";

 

 

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

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
Explorer ,
May 29, 2025 May 29, 2025

Thank you 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
Explorer ,
May 29, 2025 May 29, 2025

Will this work if the two checkboxes are named the same? I'm using them like a radio button, where you can only select one or the other. 

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 ,
May 29, 2025 May 29, 2025

Give the check boxes the same name, but different export values ("CURRENT" and "NEW").  Enter the following Mouse Up script in both check boxes:

if(event.target.value=="CURRENT")
{
this.getField("Current Amount").value = this.getField("Amount").value;
this.getField("New Amount").value = "";
}
else if (event.target.value=="NEW")
{
this.getField("Current Amount").value = "";
this.getField("New Amount").value = this.getField("Amount").value;
}
else
{
this.getField("Current Amount").value = "";
this.getField("New Amount").value = "";
}

Name the text fields as I have or change the names in the script to match the names of your text fields.

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
Explorer ,
May 29, 2025 May 29, 2025

Thank you 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 ,
May 29, 2025 May 29, 2025

You're welcome.  The difference between the script I provided and the one @Thom Parker provided is that mine is triggered by the checkbox selection (or deselection).  That means the values in the other fields will only change when the check boxes are clicked.  If you change Amount after the fact it won't change the other field values.  Thom's are triggered any time any field value changes so they will always take into account the checkbox selection and the Amount value.  However, since they are 2 separate check boxes, the user could check both.

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
Explorer ,
May 29, 2025 May 29, 2025

It isn't working for me right now, but I am pretty sure it has to do with how I named my form fields. I will continue to test until I get it right! Thank you!

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 ,
May 29, 2025 May 29, 2025

I wrote the script based on the idea that the checkboxes were named differently. However, it can be easily changed to work if they are named the same, using different export values. 

 

Assuming the same export values specified by @PDF Automation Station and a checkbox name of "check".

NOTE: the field names and export values used in the script must be verbatim those used on the form fields. Capitals count. 

 

// Script for Current Amount field
event.value = (this.getField("Check").value == "CURRENT")?this.getField("Amount").value:"";

 

// Script for New Amount field
event.value = (this.getField("Check").value == "NEW")?this.getField("Amount").value:"";

 

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

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 ,
May 29, 2025 May 29, 2025

Before you post next. Please look in the console window for reported errors.  Open the console with "Ctrl-j". 

 

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

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
Explorer ,
Jun 02, 2025 Jun 02, 2025

Hi @Thom Parker ,

Now that I have these fields working, if let's say the person checked off the New checkbox and the amount populates in the field I want it to, how can they enter another amount in the Current form field? I also put that same script for that field if the Current checkbox is clicked. It is not allowing them to type anything in there 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
Community Expert ,
Jun 02, 2025 Jun 02, 2025

The calculation script is overwritting anything that the user entered.  There are a few different ways to allow the user to enter a value into the field that is not selected.  Probably the easiest is to simple not overwrite the value in the non-selected field. This presents another problem though.  If the user selectes New, then the new feild is populated with the entered value. If they then select Current, then that field is populated with the entered value, but because the non-selected value is not overwritten the New value remains, so both fields now have the same value. 

 

Here are the modified scripts. If this does not work for you then you will need to outline the exact behavior you want. And I mean exact, down to the tiny details. 

 

 

// Script for Current Amount field
if(this.getField("Check").value == "CURRENT")
    event.value = this.getField("Amount").value;

 

// Script for New Amount field
if(this.getField("Check").value == "NEW")
    event.value = this.getField("Amount").value;

 

 

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

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
Explorer ,
Jun 02, 2025 Jun 02, 2025

Ok, thanks @Thom Parker ! I will give this a shot and let you know how it goes.

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
Explorer ,
Jun 02, 2025 Jun 02, 2025

Thank you so much! This worked wonderfully!!!

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
Explorer ,
Jun 09, 2025 Jun 09, 2025

Hi @Thom Parker ,

I had some feedback from the stakeholders re: my form. They noted that if they decided to choose "CURRENT" rather than "NEW", which they clicked on previously, the form field with the amount doesn't switch over with the switch of the checkboxes. Is there a way to toggle the new/current amount fields based on the checkbox selection as well?

 

Also, if the amount field is cleared/deleted, it doesn't delete it in the current or new form fields. 

 

 

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 ,
Jun 09, 2025 Jun 09, 2025

If the scripts were entered correctly, the value in the  Amount field will be copied to the field selected by the checkbox. If a different value is entered into the Amount field, then that value is automatically copied to the selected field. If the checkbox is changed, then the value in the Amount field is copied to the other field. If the fields are behaving in this way, then the script is operating as it should. From your description it appears that they are not operating correctly. Either the scripts were not entered correctly, or there are other scripts acting on these fields.  

Did you check the console window for errors?

 

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

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
Explorer ,
Jun 09, 2025 Jun 09, 2025

I honestly don't know how to use console windows. I can look up how to use that. I will also double-check how I applied the scripts. 

 

This is what I am getting:

Screenshot 2025-06-09 at 3.13.47 PM.pngThe top row is New and the 4th row is Current. The checkboxes are on the previous page. It works when I click New (or vice versa) and it populates in this field. But when I click on the Current checkbox without clearing the form, the number will remain there unless I clear the form first. I don't know what I did wrong. I just replaced the button names.

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 ,
Jun 09, 2025 Jun 09, 2025

Press Ctrl + j and see if there are any errors reported:

https://pdfautomationstation.substack.com/p/the-javascript-console

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 ,
Jun 09, 2025 Jun 09, 2025

The script does not automatically clear the unselected field. It only directs where the value is copied. 

Can you post the form?  Or a test form with just the fields in question on it?

 

 

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

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
Explorer ,
Jun 09, 2025 Jun 09, 2025

I've created a test form with the original form field names.

 

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 ,
Jun 09, 2025 Jun 09, 2025
LATEST

I looked through the scripts and found a couple of the relevant fields, and the scripts work correctly. 

Although, the console window shows some errors, so there is at least one script that uses a bad field name. 

 

To get this right, you need to outline the exact behavior you'd like to see in these fields. This is important because there is a natural conflict between a calculated value and a user entered value. This is complicated by switching the calculation between the fields. So, the behavior has to be thought through carefully. 

This also makes the solution somewhat complicated. I really think you need to hire a developer to do this for you.  PM me if you are interested.  There is quite a bit on this form that could be improved. 

 

 

  

 

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

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