Help with question randomisation; grouping two options within a randomisation | XM Community
Skip to main content

Hi everyone,
I wonder if you can help me with something. I have a block with 8 questions. I need the first and the last question to always be the same. I can do that easily with the built-in options that Qualtrics provides--I just move the middle 6 questions to the randomisation box in "advanced randomisation".
But here's my problem: I need two of those 6 middle responses to be paired, so that they always appear together (with one question always appearing immediately after the other) within this random order.
I assume that I would need javascript to be able to do this. Would anyone be able to help me with the kind of code I would need to do this?
Many thanks for your help with this--it's so appreciated!

Get the QID of one of the questions. In this example we'll call it QIDx. Add this JS to the other question it should be paired with:
Qualtrics.SurveyEngine.addOnload(function() {
var q = jQuery(this.questionContainer);
if(Math.random() < 0.5) q.prev().before(jQuery("#QIDx"));
else q.next().after(jQuery("#QIDx"));
});


Hi TomG, Thanks so much for your help with this. That's very kind of you.
It doesn't seem to be working for me.
So the first question of the two that always need to appear together is called EFFORT. The other, that always needs to follow is labelled "FREE". So for the javascript for FREE, I've put:
Qualtrics.SurveyEngine.addOnload(function() {
 var q = jQuery(this.questionContainer);
 if(Math.random() < 0.5) q.prev().before(jQuery("#EFFORT"));
 else q.next().after(jQuery("#EFFORT"));
});

But then I've also put both of these questions in the randomisation box in the advanced randomisation 'point and click' menu for the block in which they appear.
I am a complete novice, but could it be that the randomisation is overriding the javascript?

Any further light you could cast on this would be so appreciated.

Thanks!


https://community.qualtrics.com/XMcommunity/discussion/comment/52370#Comment_52370#EFFORT is not a valid question id. It should be something like #QIDx where x is a number. One way to find the QID is to use Piped Text to pipe the question's Question Text. The pipe string will contain the QID.


Ah, thanks for the clarification TomG, that's kind of you. I'll go away and try and do what you suggest to discover the QID (I had thought this would be anything I'd changed it to).


Hi again TomG, thanks so much for your help with this. I have tracked down the QIDs in the way you suggest (for anyone else who is reading this with a similar issue, you can also get the QIDs by exporting the data and selecting "Use internal IDs in headers" in the "more options" menu; though that's a less elegant way of doing it).
I am going to need to do this kind of randomisation with more questions in the future, and I can't see how to modify the javascript to do this at the moment. So, it would be so helpful if you might be able to give me one more example javascript, so I can try to work out how this would need to be done for other cases.
Suppose I have a block with 7 questions.
One question always needs to appear first, and one question always needs to be last. (As I said in my original message, that's easily done in advanced randomisation).
Then, the middle five questions need to appear in a random order, *but* one of these questions always needs to appear after two of these other middle questions have appeared. How would the code you gave me need to be modified for that?


https://community.qualtrics.com/XMcommunity/discussion/comment/52775#Comment_52775That would require completely different code. Try this added to the question you want to appear in position 3 through 6:
Qualtrics.SurveyEngine.addOnload(function() {
var rand = 2 + Math.floor(Math.Random() * 3);
jQuery(".QuestionOuter").eq(rand).after(jQuery(this.questionContainer));
});


https://community.qualtrics.com/XMcommunity/discussion/comment/52806#Comment_52806Thanks so much for this TomG, it's so kind of you to help with this.
When I try and put that code into the javascript for the question that needs to appear after two of the others, I get an error message saying that the javascript is invalid because there is an "unexpected identifier". Do you know what I might have done wrong here?
Many thanks again.


https://community.qualtrics.com/XMcommunity/discussion/comment/53915#Comment_53915Somehow the code got messed up when I pasted it and I didn't notice. I've fixed it in the post above.


Thanks so much TomG . Qualtrics seems to be accepting that code now; but I can't seem to get it working. With that code, the question that is supposed to be appearing after two of the others sometimes appears before those two questions.
Just to give you the full picture of what I have done:
Using the point and click 'advanced randomisation' for the block, I have put questions 2-6 in the "randomize questions" box, and leave questions 1 and 7 in the "fixed order display" box in first and last position.
Then, among the randomised questions 2-6, for the question that needs to appear after two of the others have appeared, I have put the following Javascript in the question's custom script box:
Qualtrics.SurveyEngine.addOnload(function() {
var rand = 2 + Math.floor(Math.Random() * 3);
jQuery(".QuestionOuter").eq(rand).after(jQuery(this.questionContainer));
});
I have tried the obvious solutions (such as ensuring that the edited survey has been "published"). Do you know what I might have done wrong here?
Many thanks for your help with this.


https://community.qualtrics.com/XMcommunity/discussion/comment/53919#Comment_53919Thinking about this:
I'm wondering if the advanced randomisation might be overriding the custom javascript for that question.
And I'm also wondering if the code above only requires that the question must occur in positions 3-6, rather than after two of the other questions whose order has been randomised have appeared. If that's the case, then it's possible that the two questions that it needs to be preceded by could appear *after* the question. So if that's what the code does, then I don't think it's going to do what I need. Though this might not be the case, as I don't think I understand the javascript.
Does anyone have any insights here?


Leave a Reply