randomizing a question within a loop | XM Community
Solved

randomizing a question within a loop


Badge +1

Hi !

 

I am running a survey that needs to ask the same question 20 times changing one word in the question. To do so, I have created a loop with the 20 options and selected Randomise loop order to make sure that these questions appear in a random order. 

 

Up to here everything works fine. 

 

The problem is that now I have to ask a follow up question - but I want this question to appear only for out of the 20 questions. And I want this follow up question to appear directly after the previous one, not after the whole loop is completed. 

 

For instance:

 

Question 1: What is your opinion on …A… ?

For this question we have 20 options, so: A,B,C,D,E,F,… up to 20.

I want this question to appear for the 20 options but in a random order. 

 

Question 2: Do you know where A comes from?

This question will only appear for 6 out of the 20 options, and directly after question 1. After responding to this question, the loop would continue with question 1 for other options.

 

Any help is more than welcome! Thank you!

icon

Best answer by TomG 9 March 2024, 14:47

View original

11 replies

Userlevel 7
Badge +21

You can access the current loop number using ${lm://CurrentLoopNumber} the use JS to display the question.

For example:

Qualtrics.SurveyEngine.addOnReady(function () {
const quest = this;
const qc = quest.getQuestionContainer();

// Hide the Question
qc.style.display = "none";

const currentLoop = parseInt("${lm://CurrentLoopNumber}");
const displayQ2 = [2, 3, 5];

if (displayQ2.indexOf(currentLoop) > -1) {
qc.style.display = "";
}
});

This code will display the Q2 on loops 2, 3 and 5.

Badge +1

You can access the current loop number using ${lm://CurrentLoopNumber} the use JS to display the question.

For example:

Qualtrics.SurveyEngine.addOnReady(function () {
const quest = this;
const qc = quest.getQuestionContainer();

// Hide the Question
qc.style.display = "none";

const currentLoop = parseInt("${lm://CurrentLoopNumber}");
const displayQ2 = [2, 3, 5];

if (displayQ2.indexOf(currentLoop) > -1) {
qc.style.display = "";
}
});

This code will display the Q2 on loops 2, 3 and 5.

 

thank you very much your your reply!! But then I am afraid that I am choosing that the question appears on loops 2, 3 and 5. Isn’t there a way to make it random? that is, make the timing when the question 2 appears random as well?

Userlevel 5
Badge +12

Hey @luciadome You can do this via embedded data, branch logics and randomizer as well in the survey flow, but it will be quite lengthy as there are 20 blocks.

Userlevel 7
Badge +27

@luciadome,

You could do the following:​​​​​​​

  • Add a MC question before your loop with the 20 items as choices. Add Advanced Randomization to show a subset of 6, evenly displayed. Hide the question with JS: jQuery(this.questionContainer).hide();
  • Base the loop off the hidden MC question: All Choices - Displayed & Hidden
  • Add JS to the first question in the loop (change the QID in the pipe to the QID of the hidden MC question):
Qualtrics.SurveyEngine.addOnload(function() {
var displayed = "${q://QID1/ChoiceGroup/DisplayedChoices}".split(", ");
if(displayed.includes("${lm://Field/1}")) Qualtrics.SurveyEngine.setEmbeddedData("showNext","1");
else Qualtrics.SurveyEngine.setEmbeddedData("showNext","0");
});
  • Add a page break after the first loop question
  • Add display logic to the second loop question: if showNext Is Equal to 1
Badge +1

@TomG 

 

Thank you for your response!!

The JS for hiding the question works! However when it comes to the second loop question to appear, it does not work. When I add the display logic, the embedded data “showNext” does not appear. 

I will try to explain better the situation here:

I have now two blocks of questions. 

Block 1 includes:

  • The hidden question with the 20 items as choices
  • Question 1. in the loop with the JS. “What is your opinion on …${lm://Field/1} … ?”

Qualtrics.SurveyEngine.addOnload(function() { var displayed = "${q://QID1/ChoiceGroup/DisplayedChoices}".split(", "); if(displayed.includes("${lm://Field/1}")) Qualtrics.SurveyEngine.setEmbeddedData("showNext","1"); else Qualtrics.SurveyEngine.setEmbeddedData("showNext","0"); });

  • Question 2. A question that appears the same number of times as Question 1. That is, 20. “Set …${lm://Field/1} … on the following scale:”

Block 2 includes:

  • Question 3: This is the follow-up question that I want only to appear 6 times.  MC question: “Do you know where ${lm://Field/1}  comes from?” yes/no. 
  • Question 4: This is another follow-up to Question 3 that only appears when they answer “yes” (I already put this display logic). “Please select the country for ${lm://Field/1} from the following menu.

My problem is that now the block 2 does not appear ever… I want it to appear randomly 6 of the 20 times. 

Here I attach some screenshots. 

Thank you very much!!!
 

Lucía

 

 

Userlevel 7
Badge +27

@luciadome,

A few things:

  1. Embedded data fields are only displayed in the picklist if they are defined in the survey flow. However, they don’t have to be defined in the survey flow to use them in display logic. You can type the field name.
  2. The only JS in Question 1 is the line to hide the question. However, if Question 1 is the only question on the page, it should also have JS to click the next button (i.e., this.clickNextButton();)
  3. Question 2 should be the first question in Block 2 (i.e., in the Loop & Merge block). Question 2 should include the JS that sets the value of showNext. 
Badge +1

thank you @TomG !! this was very helpful and now it works 😀

 

my last step is to create the randomized treatment at the beginning of the survey.

I not only have 20 options, but I have in total 79 groups to which individuals can be randomly assigned to. I want a participant to be randomly assigned to one of these groups. Within each group, I have a list of the 20 options we discussed earlier.The problem is that, of course, each 20 options are different depending on which group an individual is assigned to. Thus, if I am assigned to group 1, I have A1, B1, C1, and if someone else is assigned to group 2 she will have A2, B2, etc. 

Depending on which group an individual is assigned to,  she will have 20 options (A,B,C...) from which the loop & merge and the randomization of the follow-up question is based on.

 

how can i achieve this? i thought about creating embedded data depending on the group an individual is assigned to, but qualtrics does not enable Loop & Merge fields to be assigned directly from an Embedded Data string. 

thank you in advance!

 

Userlevel 7
Badge +27

You can pipe embedded data fields into a loop & merge setup.  However, in your case, I think you would pipe them into the choices of Question 1 (the hidden multiple choice question that your loop is based on).

You will have to decode the response data for the loop questions based on group assignment.

Badge +1

thanks @TomG ! 😀 Do you know how I can achieve this with JS? Which code should I use?

Userlevel 7
Badge +27

thanks @TomG ! 😀 Do you know how I can achieve this with JS? Which code should I use?

I’m not sure what ‘this’ refers to (i.e., what code is needed other than the code in the accepted answer).

Badge +1

you’re right @TomG ! I just piped the embedded data fields into my loop & merge setup and it worked!! thank you :) 

Leave a Reply