java code for "Display when CurrentLoopNumber is equal to 5" | XM Community
Question

java code for "Display when CurrentLoopNumber is equal to 5"

  • 22 June 2019
  • 5 replies
  • 15 views

Hi there,

Thanks for your attention.

I have 40 elements in my loop and merge, all randomized. I want my respondents to "rest"(answer some unrelated questions) after they finish 5 questions.

Ideally I think a command like "Display it when CurrentLoopNumber is equal to 5" can do. But I cannot use CurrentLoopNumber in the display logic. Maybe javascript can help? As I know little about Java, hope someone can help me here.

Thanks a lot!

5 replies

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var display = "${lm://CurrentLoopNumber}" == 4;
});

I tired this but failed. Looks like a incomplete code but I do not know how to adjust it 😢
Userlevel 5
Badge +6
Hi @lulul,
what exactly failed about your code? I've tried a similar one and it was OK.
I think you just have to put all the questions you want to display during the "rest" to your Loop and merge block. Then set their CSS property `display` to `none` in your external CSS using IDs of your questions like this:

`#QID17{display:none;}`
`#QID17Separator{display:none;}`

When the loop number will be 4 set their visibility to `block` again using for example `jQuery` like this:

` jQuery("#"+this.questionId).css("display", "block");`

(And then to none again.)
Userlevel 7
Badge +27
> @lulul said:
> Qualtrics.SurveyEngine.addOnload(function()
> {
> /*Place your JavaScript here to run when the page loads*/
> var display = "${lm://CurrentLoopNumber}" == 4;
> });
>
> I tired this but failed. Looks like a incomplete code but I do not know how to adjust it 😢

That isn't valid JS syntax. Try:
```
if(parseInt("${lm://CurrentLoopNumber}") == 4) {
//do something
}
```
Thanks fleb and TomG!

However, I tried the code with and without "#" and in different places (see attached picture), and it did not work. The "rest" questions were not hided.
!

Inspired by your answers, my logic to handle the problem is that:
- Hide the "rest" questions
- Display the "rest" questions when "${lm://CurrentLoopNumber}" == 4

So I tried:

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

if(parseInt("${lm://CurrentLoopNumber}") == 4) {
jQuery("#"+this.questionId).css("display", "block");
}

});

So I guess I need a CSS code to hide questions in loop and merge, and java code to display the question when CurrentLoopNumber is equal to 4. Is that appropriate? Looking forward to any comments and thanks for your help!
Userlevel 5
Badge +6
Hi @lulul,
place A is the right one for your code. The second one should be used in case you have an external CSS file. However, you skipped this `#` in your code. Therefore it can't work.
The code seems to be OK, you'll see what it will do after you correct your CSS.

Leave a Reply