Passing/retrieving responses from select2 to backend | XM Community
Solved

Passing/retrieving responses from select2 to backend


Hello,

I'm using select2 to display answer choices for a multi select question. The list of possible answers is contained in a local JavaScript array and is quite large (nearly 700 unique choices). The choices render and selected without issues. However, when a user submits the survey, the answers they selected are not saved, and the corresponding field is blank. I have several ideas, including piping the selections to a dummy question as a free text response, but I'm not sure how to implement this. Would appreciate any suggestions!


icon

Best answer by TomG 9 July 2020, 16:19

View original

4 replies

Userlevel 7
Badge +27

Yes, you've go the right idea. Copy the select2 value(s) to a hidden question input or an embedded data variable either on change or in the addOnPageSubmit function.

Thanks for your answer! I was able to retrieve the responses from the array given by invoking

select2('data')
I checked the console log and the text of the responses, however many are selected, are printed in their entirety. So far, so good. I then created an embedded data variable of type Text . In order to have these responses recorded, on the next page of the survey I have a dummy text box question (this will be packaged to the end user as "confirming their answer selections prior to submission") . However, this only yields the last selection out of the multiple choices which were entered into the select2 question on the previous page. I would like for this text box to instead contain all of the text from the answer choices that were selected.
What am I missing? I have a feeling it's something fundamental.
I tried writing the output of to another dummy textbox via jQuery but I guess that JS variables aren't propagated across questions?

Thanks again for your help!


Userlevel 7
Badge +27

realdat needs to be an array and you need to move setEmbeddedData outside the array and convert the realdat array to comma separated string using .join().

Thank you, that worked beautifully!

Leave a Reply