Conditional statement based on block loop input | XM Community
Skip to main content

Hello,

 

Hopefully this is a fairly simple query.

The background is that I have a question inside a block loop. I want to set up some javascript that will hide select options in that question depending on which particular loop is being shown.

In the below example, the block will loop through ‘Category1’, ‘Category2’ & ‘Category3’, and when the ‘Field1’ output of the loop is equal to ‘Category2’, I want to hide options 1 & 3 of this question.

 

I have used the section that hides the options on other scripts, but I am unsure how to adjust it to take the input from the block loop (rather than a normal question). Can anyone shed some light on the right formatting required?

 

Thanks.

 

 

Qualtrics.SurveyEngine.addOnload(function() {

var field1Value = "${e://Field/Field1}";

if (field1Value === "Category2") {

      jQuery("#QID2 input>choiceid='1'], #QID2 inputchoiceid='3']").closest("li").hide();

});

Try below code:

Qualtrics.SurveyEngine.addOnload(function() {

var field1Value = "${e://Field/Field1}";
field1Value = "Category2"
if (field1Value === "Category2") {

jQuery("#" + this.questionId + " input[choiceid='3']").closest("li").hide();
jQuery("#" + this.questionId + " input[choiceid='1']").closest("li").hide();

}

});

 


Hi Shashi,

 

Thanks for the responses, but have you not just permanently set ‘field1Value’ to equal "Category2"?

I need ‘field1Value’ equal whatever the field is for the loop that I am in.

i.e. if the loop is for Category1 or Category3, then options 1 & 3 won’t be hidden, but if we are in the loop of Category2, then the if-statement becomes active.

 

What I just need is the right scripting reference to set ‘field1Value’ to be whatever the field is for the different block loop we may be in.

 

 


Hi Shashi,

 

Thanks for the responses, but have you not just permanently set ‘field1Value’ to equal "Category2"?

I need ‘field1Value’ equal whatever the field is for the loop that I am in.

i.e. if the loop is for Category1 or Category3, then options 1 & 3 won’t be hidden, but if we are in the loop of Category2, then the if-statement becomes active.

 

What I just need is the right scripting reference to set ‘field1Value’ to be whatever the field is for the different block loop we may be in.

 

 

Sorry I had just added that for testing. Use below code:

Qualtrics.SurveyEngine.addOnload(function() {

var field1Value = "${e://Field/Field1}";

if (field1Value === "Category2") {

jQuery("#" + this.questionId + " inputqchoiceid='3']").closest("li").hide();
jQuery("#" + this.questionId + " inputqchoiceid='1']").closest("li").hide();

}

});

 


Thanks for that.

I just needed to change  "${e://Field/Field1}" to "${lm://Field/1}" and that coded did the job.


Leave a Reply