Search code examples
javascriptrandomqualtrics

Randomizing masked choices in Qualtrics with Javascript


I'm programming a Qualtrics survey in which several different streams of respondents will be presented with different subsets of attributes to rate in a matrix question.

There are quite a number of both attributes and questions, so to avoid spending days clicking through answer-level display logic, I've adapted an answer from @T. Gibbons (Use Javascript for Qualtrics matrix table display logic) and used Javascript to mask the subsets with the code below.

var quest = this.questionId;
var choices = this.getChoices();

var survey_stream = "${e://Field/E8}";
var stream;

var stream1 = ["1", "2", "3", "4", "5", "6"];
var stream2 = ["2", "3", "4", "7", "8", "9"];
var stream3 = ["1", "2", "3", "4", "5", "6"];

if (survey_stream=="1") {stream=stream1;}
if (survey_stream=="2") {stream=stream2;}
if (survey_stream=="3") {stream=stream3;}

var idName;
var hiddenChoices;

hiddenChoices = choices.filter(function(x) {
    return !stream.includes(x);
});

hiddenChoices.each(function(item) {
  idName = "header~"+quest+"~"+item;
  $(idName).up().hide();
});

Ideally, the remaining items shown would also be randomized, but selecting the standard randomization option for the question 'breaks' the code above by returning the right number of answer options, but the wrong subset. My guess is that it's randomizing the order of the choices before the masking script runs, so choices are already out of order before the positional references of my streams are applied.

Since I have very limited experience with Javascript and Qualtrics documentation in this area is terrible, are there any suggestions on how I might be able to adapt my code to include the proper randomization of my choice subsets?


Solution

  • For your hiddenChoices.each function try this:

    hiddenChoices.each(function(item) {
      idName = "QR~"+quest+"~"+item+"~1";
      $(idName).up('tr.ChoiceRow').hide();
    });