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

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

  • Jul 18, 2022 8:51 PM UTC
  • 5 replies
  • 179 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 ●●
  • Aug 18, 2022 3:33 PM UTC

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 ●●●●●●●●
  • Aug 18, 2022 7:05 PM UTC

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 ●●
  • Aug 19, 2022 12:02 PM UTC

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


Forum|alt.badge.img+11
  • Level 4 ●●●●
  • Sep 24, 2024 6:22 PM UTC

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 ●●●●●●●●
  • Sep 24, 2024 6:59 PM UTC

@Rod_Pestell,

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