Split multiple choice answers into separate embedded data fields | XM Community
Skip to main content

I’ve almost, but not quite, been able to find JS to accomplish this; I’m not as familiar as I would like to be.

In short, I have a very long list of 93 options in a multiple choice question with display logic based on a prior question to make it more manageable for the respondent. They can choose up to 5, although that’s probably rare. I want to split the selected choices into separate embedded data fields (i.e. Q14 = Blue, Black, Green would become color1=Blue color2=Black color3=Green).

I want to use those up-to-5 options in a subsequent question which will form the basis of a loop and merge block; I don’t want to use the first question because I wind up with hundreds of variables with block questions for each of the 93 options. I want no more than 5 sets of variables for the loop and merge block.

How can I split the selected choices into separate fields with JavaScript?

As an update, I was able to use this to split and save the selected choices as embedded data fields. It does save them on the back end, and it works on the loop and merge:

 

 However, when I use those fields as answer options, they appear blank:

Again, if I select these, they go through the loop and merge block with the appropriate name in Field1. Any insight as to why they don’t display here? I put it on a separate page from the long question as well as tried it in a new block, but got the same result.


For further context, here is the code that works if I place it in the javascript of a question on the following page, for use on the page after that:



Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")
{

var SchoolSelect = "${q://QID12/ChoiceGroup/SelectedChoices}";
var SchoolNumberSelect = "${q://QID12/SelectedChoicesRecode}";

var SchoolSelectArr = SchoolSelect.split(', ');
var SchoolNumberSelectArr = SchoolNumberSelect.split(', ');


for (var i = 0; i < SchoolSelectArr.length; i++) {
switch (i) {
case 0:
Qualtrics.SurveyEngine.setEmbeddedData("School1", SchoolSelectArr[i]);
break;
case 1:
Qualtrics.SurveyEngine.setEmbeddedData("School2", SchoolSelectArr[i]);
break;
case 2:
Qualtrics.SurveyEngine.setEmbeddedData("School3", SchoolSelectArr[i]);
break;
case 3:
Qualtrics.SurveyEngine.setEmbeddedData("School4", SchoolSelectArr[i]);
break;
case 4:
Qualtrics.SurveyEngine.setEmbeddedData("School5", SchoolSelectArr[i]);
break;
}
}

for (var i = 0; i < SchoolNumberSelectArr.length; i++) {
switch (i) {
case 0:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber1", SchoolNumberSelectArr[i]);
break;
case 1:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber2", SchoolNumberSelectArr[i]);
break;
case 2:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber3", SchoolNumberSelectArr[i]);
break;
case 3:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber4", SchoolNumberSelectArr[i]);
break;
case 4:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber5", SchoolNumberSelectArr[i]);
break;
}

}

}
});

I really don’t want the in-between page, but the fields do not populate in the next block (looks just like the image above).


Leave a Reply