Turn Questions in Randomized Block Into Choices in Same Order | XM Community
Solved

Turn Questions in Randomized Block Into Choices in Same Order


Badge

I have a block of 4 questions presented on the same page, randomized, each one with "Yes/No" response options.
After users see this block, I want to display a single question with questions as response options, in the same order.
Unfortunately I cannot use a matrix table which would have made this simple.
For example:
Block1 (Randomize order)
Have you heard of A

  • Yes/No

Have you heard of B
  • Yes/No

Have you heard of C
  • Yes/No

Have you heard of D
  • Yes/No

Block2 (display A,B,C,D in the same order as they appeared in Block1)
Single choice: Which do you have?

12 replies

Userlevel 7
Badge +21

I don't think this is possible via the default functionality in Qualtrics. However, with a little bit of JS, its doable:
Qualtrics.SurveyEngine.addOnReady(function () {
    let counter = Number("${e://Field/counter}");
    Qualtrics.SurveyEngine.setEmbeddedData("q_no_" + counter, this.questionContainer.innerText);
    counter++;
    Qualtrics.SurveyEngine.setEmbeddedData("counter", counter);
});
Have this in the JS of each of the 4 questions.
Then in the fifth question, you can have the choices as:
${e://Field/q_no_0}
${e://Field/q_no_1}
${e://Field/q_no_2}
${e://Field/q_no_3}
This will present them in the order they were initially displayed.

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/35354#Comment_35354Sorry, but you don't want to do it that way because you'll have to decode the block2 answers on the backend.
This would be better:
Add a hidden span to the randomized block 1 questions' text with the corresponding choice id for the block2 question. For example:

Add JS to one of the block1 questions to save the question order to an embedded field:
Qualtrics.SurveyEngine.addOnload(function () {
var b1ro = [];
jQuery(".b2id").each(function() { b1ro.push(this.innerText) });
Qualtrics.SurveyEngine.setEmbeddedData("b1ro",b1ro.join(","));
});
Finally, reorder the choices in your block2 question:
Qualtrics.SurveyEngine.addOnload(function() {
var cs = jQuery("#"+this.questionId+" .ChoiceStructure");
jQuery.each("${e://Field/b1ro}".split(","), function(i,val) {
cs.append(cs.find("[choiceid="+val+"]").closest("li"));
});
});

Userlevel 7
Badge +21

https://www.qualtrics.com/community/discussion/comment/35356#Comment_35356Okay. I had missed the first line. I was working with the assumption that the first four questions need to be on different pages. With all the questions on the same page, this most probably wouldn't work at all, because all the questions will recieve the value of counter as 0.
Thanks for the correction.

Badge

Hi Tom,
I've entered the code into the first block as you instructed.
For the second block with the questions as responses, do I turn on choice randomization, or leave it for the Java to govern the display?
I think I'm missing something for the 2nd block.

Userlevel 7
Badge +27

For the block2 question there is no need to turn on randomization. The JS will reorder anyway.
Since you say you think you're missing something, I assume it isn't working. First, pipe b1ro into your block2 question text to make sure it is populated correctly. It should be a comma separate string with numbers 1 through 4 in some random order.

Badge

Tom,
Yep, the piped text is displayed correctly. However, how do I display only the one corresponding answer choice for each response option? See image below:
image.png


Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/35393#Comment_35393Aha, two problems:

  1. The values should be the choice ids of the choices in the the block2 question, not A,B,C,D. They are probably 1, 2, 3, 4, but you can use your browser's inspect feature to verify. Look at the choiceid attribute in the input tags.

  2. You should not be piping values into your block2 question choices. They should just be whatever text you want the respondent to see.

Badge

Thank you! Re-numbering the questions (removing the letters) did it!
Thanks so much for your help!
image.png

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/35396#Comment_35396Based on your initial post, your block2 question is:
Which do you have?
A (choiceid = 1)
B (choiceid = 2)
C (choiceid = 3)
D (choiceid = 4)
(I'm assuming those are the choiceids, but they could be different based on how you have edited the question)
In block1, each question should have a hidden span with its corresponding block2 question choiceid. The A question would have:



Badge

Tom - I've got it figured out now.
However, I have one more question.
If I want to add a "don't know" option to the response options in Block 2, how can I do so and ensure it is in a fixed position at the end of my list?

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/35403#Comment_35403Change the block2 script to:
Qualtrics.SurveyEngine.addOnload(function() {
var cs = jQuery("#"+this.questionId+" .ChoiceStructure");
jQuery.each("${e://Field/b1ro}".split(",").reverse(), function(i,val) {
cs.prepend(cs.find("[choiceid="+val+"]").closest("li"));
});
});

Badge

Excellent! Thanks again!

Leave a Reply