Loop & Merge question cannot set embedded variable via javascript? | XM Community
Skip to main content

The below Loop&Merge question javascript does not throw an error but also does not calculate javascript variables "n" nor "vidurl" and, hence, doesn't set the embedded variables 1stVideo, etc.) . Anybody know why? It looks so simple!
--------------------------------------------------------
Qualtrics.SurveyEngine.addOnload(function()
{
  var n = "${lm://CurrentLoopNumber}";
var vidurl = "${lm://Field/1}";
 If (n = 1) {
Qualtrics.SurveyEngine.setEmbeddedData("1stVideo", vidurl);
       }
 If (n = 2) {
Qualtrics.SurveyEngine.setEmbeddedData("2ndVideo", vidurl);
       }
 If (n = 3) {
Qualtrics.SurveyEngine.setEmbeddedData("3rdVideo", vidurl);
       }
});

Your 'if' conditions are incorrect. n is a string and a condition should be a comparison (==) not an assignment (=). They should look like:
if(n=="1")


Thanks, Tom!
Unfortunately, that apparently wasn't "the" issue (albeit "an" issue).
In the following code, I see alert(n), alert(vidurl) correctly, but, as with the original code above, it doesn't enter the if statements, since alert("hello") is not displayed, nor are the embedded variables set (verified by a follow-up question).
Qualtrics.SurveyEngine.addOnload(function()
{
  var n = "${lm://CurrentLoopNumber}";
var vidurl = "${lm://Field/1}";
alert(n);
alert(vidurl);
 If (n == 1) 
alert("hello");     
Qualtrics.SurveyEngine.setEmbeddedData("1stVideo", vidurl);
 If (n == 2) 
Qualtrics.SurveyEngine.setEmbeddedData("2ndVideo", vidurl);
 If (n == 3) 
Qualtrics.SurveyEngine.setEmbeddedData("3rdVideo", vidurl);
});



BTW, I also tried with If(n=="1"), with the quotes.


I didn’t notice the first time, but JS is case sensitive so ‘If’ is not the same as ‘if’. Use ‘if’. Also, if there are multiple commands inside the if they need to be inside { }.


Thanks so much! Really appreciate you taking the time to help, Tom.


Leave a Reply