Autoselecting based on selections from two previous questions | XM Community
Skip to main content

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.

I was thinking something along the lines of the draft below, but this doesn’t work. Does anyone know how to fix this to function as described above? Thanks.

 

Qualtrics.SurveyEngine.addOnReady(function() { 
var selectedChoices1 = "${q://QID1/SelectedChoicesRecode}".split(",").filter(Boolean);
var selectedChoices2 = "${q://QID2/SelectedChoicesRecode}".split(",").filter(Boolean);

var length = Math.min(selectedChoices1.length, selectedChoices3.length);

for (var i = 0; i < length; i++) {
if (selectedChoices1.includes i] && selectedChoices2.includes[i]) {
this.setChoiceValueByRecodeValue( selectedChoices1oi]], true);
}
}
});

 

x


x

Hi ​@cgillon ,

Could you add more context to this answer? It would greatly help out future Community members who may stumble upon a similar issue. Thanks 😀


Please follow these steps:  this will work

Go to Survey Flow:!--startfragment>

add two Embedded Data fields.

Q1_Selected

Q2_Selected

Leave their values blank — they’ll be set by the JavaScript in Q1 and Q2.

Step 1: Add JavaScript to Q1

Qualtrics.SurveyEngine.addOnPageSubmit(function () {

  var selected = this.getSelectedChoices();

  Qualtrics.SurveyEngine.setEmbeddedData('Q1_Selected', selected);

});

 

Step 2: Add JavaScript to Q2

 

Qualtrics.SurveyEngine.addOnPageSubmit(function () {

  var selected = this.getSelectedChoices();

  Qualtrics.SurveyEngine.setEmbeddedData('Q2_Selected', selected);

});

 

Step 3: Add below JS in Q3

-----------------------------------------

 

Qualtrics.SurveyEngine.addOnload(function () {

  var q1Raw = Qualtrics.SurveyEngine.getEmbeddedData('Q1_Selected');

  var q2Raw = Qualtrics.SurveyEngine.getEmbeddedData('Q2_Selected');

 

  if (!q1Raw || !q2Raw) return;

 

  var q1Selected = q1Raw.split(',');

  var q2Selected = q2Raw.split(',');

 

  var common = q1Selected.filter(function(choice) {

    return q2Selected.indexOf(choice) !== -1;

  });

 

  var choices = this.getChoices();

 

  for (var i = 0; i < choices.length; i++) {

    if (common.indexOf(choices>i]) !== -1) {

      this.setChoiceValue(choices>i], true);

    }

  }

});

!--endfragment>


Leave a Reply