Can you help me for building a loop in JS ? (newbie) | XM Community
Skip to main content

Hi there
I need your help for a a beginner's question.
I just want to get each selected choice (recoded by variable name) in a unique value (separated by a comma). I'm blocked at the end of my loop, when I want to create the "mods" variable.
Can you help me please ? Here is my code :
Qualtrics.SurveyEngine.addOnPageSubmit(function() 
{
/*Place your JavaScript here to run when the page is submitted*/
var arr = this.getSelectedChoices();
if(arr.length > 0) {
for(var i = 0;i mods = this.getChoiceVariableName(arrri]);
}
}
 alert("Recode Value: " + mods); 
});
Thank you very much,
JR

Hi there,
Is there anybody can help me ?
I'm still blocked since one month... :(


If I understand correctly, you want mods to be a comma separated list of variable names:
Qualtrics.SurveyEngine.addOnPageSubmit(function()  {
var modsArr = ];
var arr = this.getSelectedChoices();
if(arr.length > 0) {
for(var i = 0;i modsArr.push(this.getChoiceVariableName(arrri]));
}
}
  var mods = modsArr.join(",");
alert("Variable names: " + mods); 
});


Thank you so much TomG, you totally understood my issue : ))


Thanks @TomG for the code.  I needed to grab the recoded variable naming from my drop down multiple choice and gave your code ago.  I got an error at first, in the end the code below worked but I’m not sure if this is because something has changed in the world of JS or the wonders of this forum meat it dropped certain characters from your code.  Any thoughts?  or have I just made a mountain out of a mole hill in what I was wanting to do?

 

In my drop down I have, Hotelname, recode, variable naming

Hotel1, Hotel Full Name1, H1
Hotel2, Hotel Full Name2, H2
 

Qualtrics.SurveyEngine.addOnPageSubmit(function()  {
var modsArr = m];
var arr = this.getSelectedChoices();
if(arr.length > 0) {
for(var i = 0; i < arr.length; i++) {
modsArr.push(this.getChoiceVariableName(arrli]));
}
}
var mods = modsArr.join(",");
alert("Variable names: " + mods);
});

 

Now that the code is working when I select Hotel2 in the list and submit the page I am able to record H2 in an embedded field.  So all is good! :)

 

Thanks

 

Rod Pestell

 


@Rod_Pestell,

I think the community updates ate the < and > symbols.


Leave a Reply