javascriptqualtrics

Javascript in Qualtrics: Referencing the displayed order of a choice


For a question, I have 4 choices (alpha, beta, gamma, delta) displayed in random order through choice randomization. I want the displayed order of these choices to be captured in embedded data fields, to be used later in the survey (to study how order in which choices are displayed affects recall, for example). For instance, if the choices were displayed in the order

beta gamma alpha delta

I want embedded data fields pos_beta = 1, pos_gamma = 2, pos_alpha=3 and pos_delta=4.

If they were displayed

gamma alpha beta delta

I want embedded data fields pos_gamma = 1, pos_alpha = 2, etc

Easiest way to implement this? (Also, could the answer be generic enough to capture if the choices had random text around the keywords - "blah blah gamma blah blah" etc?)

I guess I have to loop through each choice using some version of:

jQuery("#"+this.questionId+" [type=radio]").each(function(i) {
}

and within the loop check sequentially if "alpha", "beta" etc is contained in the choice text, and then assign i to the corresponding embedded data field. Is this approach correct, and if yes, how do I check if a specific substring is contained in the choice text?

Thanks in advance!


Solution

  • You can do this:

    Qualtrics.SurveyEngine.addOnload(function() {
      var qobj = this;
      jQuery(this.questionContainer).find("[choiceid]").each(function(i) {
        var cid = jQuery(this).attr("choiceid");
        Qualtrics.SurveyEngine.setEmbeddedData("pos_"+qobj.getChoiceVariableName(cid),i+1);
      });
    });
    

    You can decouple variable names from the displayed choice labels by setting them using 'Recode Values'.