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

How do I use a radio button selection to populate text field with what was selected from a dropdown?

Explorer ,
Apr 28, 2023 Apr 28, 2023

I have multiple dropdown boxes, each having seven options. Next to each dropdown box there are "Yes" and "No" radio buttons. The user needs to select one of the options from each dropdown and then use a radio button to select "Yes" or "No". If "Yes" is selected, I need the option from the dropdown selection to populate in a text field, If "No is selected, then nothing needs to happen.  

Is this possible? If so, is it possible to have just one text field with all of the "Yes" selections? 

TOPICS
Create PDFs , How to , JavaScript , PDF forms
7.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
1 ACCEPTED SOLUTION
Community Expert ,
May 15, 2023 May 15, 2023

You've inserted multiple errors into the code when you edited it, which would also trigger error messages to appear in the JS Console.

Use this, instead:

 

var values = [];
for (var i=1; i<=12; i++) {
	if (this.getField("Choice"+i).valueAsString=="Yes") {
		values.push(this.getField("Dropdown"+i).valueAsString);
	}
}
event.value = values.join(", ");

 

 

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 28, 2023 Apr 28, 2023

When you say "nothing needs to happen", do you mean the text field should maintain its value, no matter what it is? And what are the names of the fields involved?

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 ,
Apr 28, 2023 Apr 28, 2023

That is correct. When the "No" radio button is selected, the value of the selected option in the dropdown does not populate in the text field. 

The dropdown fields are "dropdown1", "dropdown2", "dropdown3", etc. The radio buttons are "choice1", "choice2", choice3, etc. The text field is "summary".

For example, the user selects the "English" option from dropdown1 and then they select "Yes" from the choice1 radio button options, "English" populates in the text field summary. If the user selects "Math" from dropdown2 and then selects "Yes" from the choice2 radio buttons, then "Math" would also appear in the text field summary. If on dropdown3 the user selects "Science" and then selects the "No" option from the choice3 radio, then "Science" does not appear in the summary text field, but "English" and "Math" would still be in the summary text field. The "No" option of the radio button only means that the selected option in the dropdown does not get populated in the summary text field. 

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 29, 2023 Apr 29, 2023

But should the user be able to manually change the value of the text field, even if some of the radio-buttons are ticked as Yes?

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 01, 2023 May 01, 2023

No, the user only has the choices in the dropdown. The text field would be read only. If they select "Yes" the dropdown option appears in the text field. If they change their mind and select "No" for a dropdown they previously answered "Yes" to, the text for that answer would not stay in the text field.  

acrobat.jpg

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 08, 2023 May 08, 2023

OK, then you can use something like this code as the custom Calculation script of the first text field, for example:

 

event.value = this.getField("choice1").valueAsString=="Yes" ? this.getField("dropdown1").valueAsString : "";

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 11, 2023 May 11, 2023

This works great! Is there a way to have multiple "Yes" answers populate the choice value in the same text field as a list or do I need to have a separate text field for each possible "Yes" response?

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 11, 2023 May 11, 2023

Something like this?

 

 

var values = [];
for (var i=1; i<=3; i++) {
	if (this.getField("choice"+i).valueAsString=="Yes") {
		values.push(this.getField("dropdown"+i).valueAsString);
	}
}
event.value = values.join(", ");

 

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 Expert ,
May 11, 2023 May 11, 2023

Change 'values' to array.

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 11, 2023 May 11, 2023

Correct, 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
Explorer ,
May 12, 2023 May 12, 2023

I've tried a few different variations with replacing 'values' with array and can't seem to get it to work. What am I missing? 

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 12, 2023 May 12, 2023

What happens when you use it, exactly?

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 12, 2023 May 12, 2023

The word from the dropdown menu does not appear in the "Summary" text field. The JavaScript Debugger indicates "TypeError: this.getField(...) is null
3:Field:Calculate
TypeError: this.getField(...) is null
3:Field:Calculate
TypeError: this.getField(...) is null
3:Field:Calculate"

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 12, 2023 May 12, 2023

This means one of the field names you specified is incorrect. Double-check everything, including upper/lower-case letters, spaces, etc. Even a single character off will cause it to malfunction.

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 12, 2023 May 12, 2023

I'm still new at this so I think that there is some other basic thing I am missing. I put the formula below in the previously mentioned "Summary" box where I want all the "Yes" answers to go, double checked the names of the fields that go into the formula, and tried different variations of the formula. 

var array = [];
for (var i=1; i<=3; i++) {
if (this.getField("choice"+i).valueAsString=="Yes") {
values.push(this.getField("dropdown"+i).valueAsString);
}
}
event.value = values.join(", ");

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 12, 2023 May 12, 2023

Then you'll need to share the file for further help.

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 12, 2023 May 12, 2023

Change the first line back to what I posted above (after the edit).

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 15, 2023 May 15, 2023

I still can't get it to work. I attached a copy of the file. Any feedback would be greatly appreaciated. 

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 15, 2023 May 15, 2023

You've inserted multiple errors into the code when you edited it, which would also trigger error messages to appear in the JS Console.

Use this, instead:

 

var values = [];
for (var i=1; i<=12; i++) {
	if (this.getField("Choice"+i).valueAsString=="Yes") {
		values.push(this.getField("Dropdown"+i).valueAsString);
	}
}
event.value = values.join(", ");

 

 

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 15, 2023 May 15, 2023
LATEST

You are awesome! 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