Skip to main content
Inspiring
April 11, 2024
Answered

Trouble with setting dropdown (combo) field values from text field inputs

  • April 11, 2024
  • 1 reply
  • 586 views

Hello helpful people!

 

I have been flummoxed by this problem all afternoon. I would like to have the user enter data into 5 fields named Result1, Result2, etc and for their answers to be ported to ResultDropdown1, ResultDropdown2, etc (5 total dropdowns, all populated the same way, but user can select different things from them).

 

this.getField("SkillDropdown1").setItems(["(choose one)",this.getField("Skill1").value,this.getField("Skill2").value,this.getField("Skill3").value,this.getField("Skill4").value,this.getField("Skill5").value]);

 

This code works as a custom calculation script to populate them. However, if I try to select an answer when testing, it reverts to the default of "(choose one)". Choosing "commit value immediately" doesn't seem to have any effect.

 

If I move this code into a custom validation instead, the whole program hangs with the weird keyboard looking icon taking over the mouse pointer and I have to force close.

 

I tried moving the custom validation code into one of the Results fields, but while it populated the "(choose one)" selection, the remaining selections did not push their values into the dropdown.

 

Any guidance? Perhaps a document-level script? How would I set that up if it's something other than just "paste the code as-is into the document level script editor"?

 

Thank you!

This topic has been closed for replies.
Correct answer Thom Parker

Calculation scripts are run every time any field on the form is changed, so it can't be used for this purpose without some qualication. It's also going to always set the value of the dropdown to the first entry in the "setItems" list. 

 

There are two choices,

1. Put code to set the dropdown items into the custom validation script of every "Skill" field. However the code has to be modified for each field. The "getField" for the field the script is in must be replaced with "event.value". because doc.getField cannot be used to get the value of the field the script is in. 

 

2. Put the code into the dropdown calculation script with qualifiers to prevent it from running if the calculation is triggered by any field other than the skill fields. Like this:

 

 

if((event.source.name == "Skill1") || (event.source.name == "Skill2") || (event.source.name == "Skill3") || (event.source.name == "Skill4") || (event.source.name == "Skill5"))
{
    this.getField("SkillDropdown1").setItems(["(choose one)",this.getField("Skill1").value,this.getField("Skill2").value,this.getField("Skill3").value,this.getField("Skill4").value,this.getField("Skill5").value]);
}

 

 

If I was doing this I'd write generalized document level functions so they would work for all cases. That way the code is in only one place, but used in all the fields. Doing this requires a field naming scheme that allows the script to relate the name of the field the script is in, to the other fields that are used in the script. 

You might want to watch the video here on field naming:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

  

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
April 12, 2024

Calculation scripts are run every time any field on the form is changed, so it can't be used for this purpose without some qualication. It's also going to always set the value of the dropdown to the first entry in the "setItems" list. 

 

There are two choices,

1. Put code to set the dropdown items into the custom validation script of every "Skill" field. However the code has to be modified for each field. The "getField" for the field the script is in must be replaced with "event.value". because doc.getField cannot be used to get the value of the field the script is in. 

 

2. Put the code into the dropdown calculation script with qualifiers to prevent it from running if the calculation is triggered by any field other than the skill fields. Like this:

 

 

if((event.source.name == "Skill1") || (event.source.name == "Skill2") || (event.source.name == "Skill3") || (event.source.name == "Skill4") || (event.source.name == "Skill5"))
{
    this.getField("SkillDropdown1").setItems(["(choose one)",this.getField("Skill1").value,this.getField("Skill2").value,this.getField("Skill3").value,this.getField("Skill4").value,this.getField("Skill5").value]);
}

 

 

If I was doing this I'd write generalized document level functions so they would work for all cases. That way the code is in only one place, but used in all the fields. Doing this requires a field naming scheme that allows the script to relate the name of the field the script is in, to the other fields that are used in the script. 

You might want to watch the video here on field naming:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

  

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
April 12, 2024

Wonderful, thank you so much, Thom! That makes sense that the calculation would run every time, I appreciate the code showing me how to bypass that problem and only fire when the specific field inputs are changed. Worked brilliantly!

 

Also thank you so much for the video on field naming! I try to keep my field names very understandable to myself as I'm calling them in other places (nothing makes me more annoyed at sloppy form field design than cracking open someone else's and seeing "Text field 37"). I look forward to watching it to improve my game further when I have the chance!

 

Best to you and thanks again!