I am having trouble with a bit a script that I was hoping someone could help me with.
I have 3 multiple choice questions, all with the same response options. QID1 & QID2 are independent question, where respondents may select any number of different options. Then, when we get to QID3, I want any common options that have been selected across QID1 & QID2 to be autoselected at this question (so that I can use this to control subsequent working).
e.g. if ‘Brand 1’, ‘Brand 3’ & ‘Brand 6’ are selected at QID1 AND ‘Brand 3’, ‘Brand 6’, ‘Brand 10’ & ‘Brand 12’ are selected at QID2, then ‘Brand 3’ & ‘Brand 6’ should be automatically selected when QID3 loads.
The script below works when all the 3 questions have the same value format, but in the situation I need this for, the various response options have been carried forward multiple times, changing the value formats (i.e. QID1 & QID2 have the value format of “xx1”, while the question I need to autoselect is formatted as “xxx1”). The extra “x” is causing the script not to work. Does anyone know how to work around this?
Thanks.
Qualtrics.SurveyEngine.addOnReady(function() {
var selectedChoices1 = "${q://QID1/SelectedChoices}".split(",").map(choice => choice.trim()).filter(Boolean);
var selectedChoices2 = "${q://QID2/SelectedChoices}".split(",").map(choice => choice.trim()).filter(Boolean);
var commonChoices = selectedChoices1.filter(choice => selectedChoices2.includes(choice));
var qinfo = this.getQuestionInfo();
commonChoices.forEach(choice => {
var choiceEntry = Object.entries(qinfo.Choices).find(([key, value]) => value.Text === choice);
if (choiceEntry) {
var choiceID = choiceEntry[0];
this.setChoice(choiceID, true);
}
});
});