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

Can you help me for building a loop in JS ? (newbie)

  • July 18, 2022
  • 5 replies
  • 135 views

JR33
QPN Level 2 ●●
Forum|alt.badge.img+10
  • QPN Level 2 ●●

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(arr[i]);
}
}
 alert("Recode Value: " + mods); 
});
Thank you very much,
JR

5 replies

JR33
QPN Level 2 ●●
Forum|alt.badge.img+10
  • Author
  • QPN Level 2 ●●
  • August 18, 2022

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


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • August 18, 2022

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(arr[i]));
}
}
  var mods = modsArr.join(",");
alert("Variable names: " + mods); 
});


JR33
QPN Level 2 ●●
Forum|alt.badge.img+10
  • Author
  • QPN Level 2 ●●
  • August 19, 2022

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


Forum|alt.badge.img+11
  • Level 4 ●●●●
  • September 24, 2024

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 = [];
var arr = this.getSelectedChoices();
if(arr.length > 0) {
for(var i = 0; i < arr.length; i++) {
modsArr.push(this.getChoiceVariableName(arr[i]));
}
}
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

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 24, 2024

@Rod_Pestell,

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