Conditional display upon iteration in loop | XM Community
Skip to main content
Hi, I was wondering if someone could help me with the following:

I have a loop & merge item of which the order is randomized. I'd like to show a descriptive text question in all iterations except for the first (i.e. "You are going to answer these sets of questions another time ..."). Is there I way I can 'tap into' the loop counter using display logic? The only option I found in there is selecting a specific loop item in which the question should be displayed, but the fact that my loop is randomized makes this unusable.
I'd suggest using JavaScript. Here's what I'm picturing:



1 - Create an embedded data field in your survey flow and set it default to 1.

2 -On the screen the text displays on, add something like a span to put the display text into:



`<span id="descText"> </span>`



3 -onReady you do something like this:



`var loopnum = "${e://Field/loopnum}";//Embedded data loop counter`

`if(loopnum<2) { //If this is the first loop then...`

` document.getElementById('descText').innerHTML = "";`

`} else { //Otherwise this is not the first loop so we display the text...`

` document.getElementById('descText').innerHTML = "You are going to answer these sets of questions another time...";`

`}`



4 - onPageSubmit at the end of the loop you will want to do something like this to increment the counter:



`Qualtrics.SurveyEngine.addOnPageSubmit(function() `

`{`

` var loopnum = "${e://Field/loopnum}";`

` loopnum = loopnum+1;`

` Qualtrics.SurveyEngine.setEmbeddedData("loopnum", loopnum);`

`});`
Thanks, I already solved this with a similar solution, without using javascript but some smart usage of embedded data fields.

dschreij I am curious to know how you solved this since I have the same problem!


I don't remember exactly to which survey this question related to exactly, as I've created a lot of similar surveys with complex loop structures in the meantime. If I remember correctly, if I didn't want to show a question on a certain iteration, I used this piece of code:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
if (parseInt("${lm://CurrentLoopNumber}") === 1) {
jQuery("#"+this.questionId).hide();
this.clickNextButton()
}
});
where the number after the

===
of course corresponds to the iteration you want to skip. Basically this script simply hides the question from view and clicks the next button, effectively skipping the slide.


dschreij Thanks, how do I change it so that it does that for a range of values, or merely for all loop counts under/over a certain value?

Is it something like ==< ? I am not familiar with using three equal signs in this manner.


Haha yes, that's a JavaScript thingy (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators) but you might as well use double quotes in this situation. You could use a range operator as you suggested. If you really only want to skip the display on specified iterations, you could use

if ([20, 40, 60].includes(parseInt("${lm://CurrentLoopNumber}"))) {...}
Beware though that the includes method does/did not exist on Internet Explorer yet, so this will only work in modern browsers (which people should use anyway).


Leave a Reply