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

Populating Text Field with Multiple Dropdown Selections

Community Beginner ,
Apr 10, 2024 Apr 10, 2024

Hello,

 

I have a dropdown (Reasons) with multiple selections (Test1, Test2, Test3). I want to be able to select one option (Test1), have that value populate a text field (ReasonsList), return to a new line or have a comma, and then populate another value when I select another option (selecting Test2 after Test1 would give me "Test1, Test2" in the text field).

 

Is this possible? I've found some Javascript in other community posts, but I'm not able to get it to work for my case.

TOPICS
Create PDFs , Modern Acrobat , PDF , PDF forms
1.1K
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 ,
Apr 10, 2024 Apr 10, 2024

As the custom Validation script of the drop-down enter the following code:

 

 

if (event.value) {
	var reasonsListField = this.getField("ReasonsList");
	if (reasonsListField.valueAsString=="") reasonsListField.value = event.value;
	else reasonsListField.value+=", " + event.value;
}

 

 

Make sure to tick the option to commit the selected value of the drop-down automatically if you want the text field's value to update as soon as you make a selection. Otherwise it will only work when you exit the drop-down field.

 

[Edited: Code fixed]

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 10, 2024 Apr 10, 2024

There is an error in last line: else reasonsListField.value+=", " + event.value;

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 10, 2024 Apr 10, 2024

As the custom Validation script of the drop-down enter the following code:

 

 

if (event.value) {
	var reasonsListField = this.getField("ReasonsList");
	if (reasonsListField.valueAsString=="") reasonsListField.value = event.value;
	else reasonsListField.value+=", " + event.value;
}

 

 

Make sure to tick the option to commit the selected value of the drop-down automatically if you want the text field's value to update as soon as you make a selection. Otherwise it will only work when you exit the drop-down field.

 

[Edited: Code fixed]

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 ,
Apr 10, 2024 Apr 10, 2024

Thank you for the quick response. This handles the first part where it throws the value into the text field. Is it possible then to add a line break or comma, followed by a second selection value if I were to then click on "Test2" in the 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
Community Expert ,
Apr 10, 2024 Apr 10, 2024

There is an error in last line: else reasonsListField.value+=", " + 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
Community Beginner ,
Apr 10, 2024 Apr 10, 2024

Thank you both for the responses. It looks like it's doing what I need it to. \o/

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 10, 2024 Apr 10, 2024

If you don't want to allow duplicates, here is a modified script:

 

if (event.value) {
var reasonsListField = this.getField("ReasonsList");
if (reasonsListField.valueAsString == "") {
reasonsListField.value = event.value;} 
else {
var currentValues = reasonsListField.value.split(", ");
if (currentValues.indexOf(event.value) == -1) {
reasonsListField.value += ", " + 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
Community Expert ,
Apr 10, 2024 Apr 10, 2024
LATEST

Good catch! I fixed it 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