How do you carry a value from a loop & merge into a new block? | XM Community
Skip to main content

Hello,
I have a block where I utilize Loop & Merge in order to choose one value from a set of values. As such, the loop only occurs once.
For example, Field 1: 10, 20, 30, 40, 50.
1 random loop may choose 20.
I want to input text in subsequent blocks that incorporates 20 or whatever was chosen in the loop.
-------------------------------------------------------------------------------------------------------------
Example:
Block 1: 20 is chosen from [10,20,30,40,50] by loop & merge function
Q1: "You are receiving $20 for this study"
Final Block:
Qi: "Thank you for participating in this study. You will receive $20."
-------------------------------------------------------------------------------------------------------------
I am hoping to do this because the random amount reappears in many blocks. With something like display logic or then if branches, I would have to create r x q iterations. Where r is the amount of possible random values and q is every question/text where that value appears. Right now, that is 8 x 20......

A loop & merge probably wasn't the best choice for picking a random number. Anyway, now that you've done it that way, the easiest way to pipe the random value going forward is to add JS to your loop question to save it to an embedded data field.
Qualtrics.SurveyEngine.addOnload(function() {
Qualtrics.SurveyEngine.setEmbeddedData("randAmt","${lm://Field/1}");
});


https://community.qualtrics.com/XMcommunity/discussion/comment/42886#Comment_42886So easy, thanks so much!
It is my first time using qualtrics or Java, if there is an easier way to randomly choose from a predetermined set, please let me know!



https://community.qualtrics.com/XMcommunity/discussion/comment/42889#Comment_42889There are a number of ways, but one that doesn't require any JS is to use a survey flow randomizer and add embedded blocks under it that set the value of randAmt to each of your values. Then set the randomizer to display 1 evenly presented.


https://community.qualtrics.com/XMcommunity/discussion/comment/42886#Comment_42886Hi TomG. What if the loop & merge randomly picked more than one value out of the possible options. Is it possible to carry forward those values selected?
My set has 50 possible values. In my survey, respondents have to choose between two options for 10 periods. If they choose option A, a certain value is shown. If they choose option B, one of the 50 possible values is randomly selected and shown to the respondent.
At the end of the survey, one of the 10 choices is selected randomly. Hence, I would need to know if the respondent chose A or B for that specific choice. If the choice was B, I would also need the randomly chosen value shown.
I'm sorry if this may be unrelated to the original post, I'm new in Qualtrics, but I thought this was somehow related.
Thank you!


https://community.qualtrics.com/XMcommunity/discussion/comment/45553#Comment_45553I'm not sure I'm clear on the details on what you are trying to do, but I'll make some assumptions and give it a shot:

  1. Create a multiple choice question with your 50 possible values, add advanced randomization to display 10 of them, hide the question with JS (and click the next button if it is on its own page)

  2. The next block is a loop based off the displayed choices in (1) above. In it is your A/B question with the A value shown in choice A and ${lm://Field/1} shown in choice B.

  3. The next block is a multiple choice question with 50 choices whose values are the piped selected choices from your A/B question in (2) above for each loop. Each choice has display logic based on whether it was displayed in (1) above. Use advanced randomization to randomly display 1 choice. Use JS select the only choice and click next.

  4. Pipe the selected choice from (3) above - this is the one random choice out of the 10 A/B choices. If it happens to be a B choice, it will contain the value from the 50 possible values.




https://community.qualtrics.com/XMcommunity/discussion/comment/45557#Comment_45557Thank you, Tom! Most of your assumptions were correct, the only difference is that respondents do not know the value of B until they pick it. Thus, they first choose between A and B. If the answer is B, then they see the randomly picked value.
However, I'm following your suggestions and I'm almost done! I have three questions.
1) This is the JV I'm using to hide the first question and click the next button. When I'm previewing the question, it seems to take a few seconds. Am I using the correct code?
Qualtrics.SurveyEngine.addOnload(function() {
jQuery("#"+this.questionId).hide();
this.clickNextButton();
});

2) By following step 3, I am using the selected choices. Hence, my code is only showing A or B (given the change I mentioned earlier). I'm also new to JS, do you have a suggestion on what the code should look like such that it selects the only choice?
3) What changes can we do such that steps 3 and 4 show the actual value (given the A/B choices)? I added two text questions in the Loop block being displayed after the A/B decision: one for A, with a certain value, and one for B, with ${lm://Field/1}.

Also, do you think it is possible to show the respondent which loop was selected, besides the response from that loop? I mean, something like "the randomly chosen decision was ##, and the value chosen was XX"?

Thank you, Tom!


1) This is the JV I'm using to hide the first question and click the next button. When I'm previewing the question, it seems to take a few seconds. Am I using the correct code?

Yes, but since you are clicking the next button you don't have to hide the question. If you want the page hidden you can add
to the question text.
2) By following step 3, I am using the selected choices. Hence, my code is only showing A or B (given the change I mentioned earlier). I'm also new to JS, do you have a suggestion on what the code should look like such that it selects the only choice?

Query("#"+this.questionId+" input[type=radio]").prop("checked",true);
3) What changes can we do such that steps 3 and 4 show the actual value (given the A/B choices)? I added two text questions in the Loop block being displayed after the A/B decision: one for A, with a certain value, and one for B, with ${lm://Field/1}.

You need the value to be an answer to a question in the loop. I would add a hidden question on the same page as the A/B question with the A/B values piped as choices. JS would select the choice that matches the A/B selection
Also, do you think it is possible to show the respondent which loop was selected, besides the response from that loop? I mean, something like "the randomly chosen decision was ##, and the value chosen was XX"?

I'm not sure if
##
should be 1-50 from the list of 50 or 1-10 in the order the loops were presented. Either way, it is possible but quite complicated. Frankly, it would take more time than I'm willing to spend to explain it.


Hi TomG thank you so much!
Well, the ## should be 1-10. But I get it might be complicated.
One final question, any suggestions on that last code? Such that JS selects the choice that matches the A/B selection?
Thank you!!


https://community.qualtrics.com/XMcommunity/discussion/comment/45570#Comment_45570Add a MC question immediately after adn on the same page as the A/B question. The choice values match the A/B order. Add the JS below to the A/B question (untested):
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var q = jQuery("#"+this.questionId);
var cid = q.find("input:checked").attr("choiceid");
q.nextAll(".QuestionOuter").first().find("input[choiceid="+cid+"]").prop("checked",true);
});


Leave a Reply