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

Looking for a better way of pre populating fields from drop down selection

New Here ,
Apr 16, 2024 Apr 16, 2024

Hello All,

 

I have been creating forms for a while with the help of this great community, however im wondering if there is a simpler/better way of creating pre populated forms based on a drop down selection.

 

Example:

Drop down slection choices - Model 1, Model 2 or Model 3

For arguments sake lets say there are 3 fields which will require pre-populated text based on the above drop down selection.  Make, Manufacturer and Tax Class.

 

The current way I would create this would be to have stacked fields on top of each other, named using the Group. prefix, then code using hidden/visible based on what was required.  While this works it does get very confusing creating/editing when there are 20 - 30 fields each needing 4 or 5 fields to be stacked on top of each other and it also makes the form very slow and clunky.

Would I be able to use something like variable code to pre populate  a single set of fields with differing data based on the export of the drop down?  If this is possible could someone kindly give an example of the code?  I'd assume that the code would then be used in the custom calculation box of each field?

------------------------------------------------------------------------
[Sighs] - Trying my hardest to learn all this
TOPICS
JavaScript , PDF forms
416
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 ,
Apr 16, 2024 Apr 16, 2024

Yes, it's certainly possible. There's really no need to have multiple sets of fields one on top of another.

The basic code to do it is the following (to be used as the Validation script of the drop-down field):

if (event.value=="Model 1") {
	this.getField("Make").value = "Make 1";
	this.getField("Manufacturer").value = "Manufacturer 1";
	this.getField("Tax Class").value = "Tax Class 1";
} else if (event.value=="Model 2") {
	this.getField("Make").value = "Make 2";
	this.getField("Manufacturer").value = "Manufacturer 2";
	this.getField("Tax Class").value = "Tax Class 2";
} else if (event.value=="Model 3") {
	this.getField("Make").value = "Make 3";
	this.getField("Manufacturer").value = "Manufacturer 3";
	this.getField("Tax Class").value = "Tax Class 3";
} else {
	this.getField("Make").value = "";
	this.getField("Manufacturer").value = "";
	this.getField("Tax Class").value = "";
}

This is a bit cumbersome, of course, and can be replaced with a more neat data-model, or even with data read from an attached text file, but that requires a bit more complicated script. If you're interested in such a solution see this (paid-for) tool I've developed exactly for this task: https://www.try67.com/tool/acrobat-populate-fields-from-dropdown

 

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 ,
Apr 16, 2024 Apr 16, 2024
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
New Here ,
Apr 16, 2024 Apr 16, 2024

Thank you Bernd.  The link is very useful but im not looking to populate from other field values.
I will however be using this for another form idea I have,

------------------------------------------------------------------------
[Sighs] - Trying my hardest to learn all this
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 ,
Apr 16, 2024 Apr 16, 2024

Yes, it's certainly possible. There's really no need to have multiple sets of fields one on top of another.

The basic code to do it is the following (to be used as the Validation script of the drop-down field):

if (event.value=="Model 1") {
	this.getField("Make").value = "Make 1";
	this.getField("Manufacturer").value = "Manufacturer 1";
	this.getField("Tax Class").value = "Tax Class 1";
} else if (event.value=="Model 2") {
	this.getField("Make").value = "Make 2";
	this.getField("Manufacturer").value = "Manufacturer 2";
	this.getField("Tax Class").value = "Tax Class 2";
} else if (event.value=="Model 3") {
	this.getField("Make").value = "Make 3";
	this.getField("Manufacturer").value = "Manufacturer 3";
	this.getField("Tax Class").value = "Tax Class 3";
} else {
	this.getField("Make").value = "";
	this.getField("Manufacturer").value = "";
	this.getField("Tax Class").value = "";
}

This is a bit cumbersome, of course, and can be replaced with a more neat data-model, or even with data read from an attached text file, but that requires a bit more complicated script. If you're interested in such a solution see this (paid-for) tool I've developed exactly for this task: https://www.try67.com/tool/acrobat-populate-fields-from-dropdown

 

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
New Here ,
Apr 16, 2024 Apr 16, 2024

Thank you Try.  This is exactly what I was after.

 

------------------------------------------------------------------------
[Sighs] - Trying my hardest to learn all this
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 ,
Apr 16, 2024 Apr 16, 2024

You can also create an array of objects to keep list organized and easily replacable like this:

var cList = [
{model: "Civic",   make: "Honda",  manufacturer: "Honda Motor Co., Ltd.",    taxClass: "Standard"},
{model: "Corolla", make: "Toyota", manufacturer: "Toyota Motor Corporation", taxClass: "Reduced"},
{model: "Model S", make: "Tesla",  manufacturer: "Tesla, Inc.",              taxClass: "Zero"}
];


//Don't change this part of script, just update the list above.
var found = false;
for (var i in cList) {
 if (event.value == cList[i].model) {
  this.getField("Make").value = cList[i].make;
  this.getField("Manufacturer").value = cList[i].manufacturer;
  this.getField("Tax Class").value = cList[i].taxClass;
  found = true;
  break;}}
if (!found) {
    this.getField("Make").value = "";
    this.getField("Manufacturer").value = "";
    this.getField("Tax Class").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
New Here ,
Apr 16, 2024 Apr 16, 2024

Thank you Nesa.  I have 17 fields currently on this form so will use trys method for speed but I will definitely look into using your code once i get my head around it.

 

------------------------------------------------------------------------
[Sighs] - Trying my hardest to learn all this
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 ,
Apr 16, 2024 Apr 16, 2024
LATEST

See this article:

https://acrobatusers.com/tutorials/change_another_field/

And this site:

https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm

 

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