Skip to main content
Inspiring
April 28, 2023
Answered

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

  • April 28, 2023
  • 1 reply
  • 7175 views

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? 

This topic has been closed for replies.
Correct answer try67

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


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(", ");

 

 

1 reply

try67
Community Expert
Community Expert
April 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?

Inspiring
April 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. 

try67
Community Expert
Community Expert
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?


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