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

Loop & Merge question cannot set embedded variable via javascript?

  • August 6, 2021
  • 5 replies
  • 16 views

Forum|alt.badge.img

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);
       }
});

5 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • August 6, 2021

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")


Forum|alt.badge.img
  • Author
  • August 7, 2021

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);
});



Forum|alt.badge.img
  • Author
  • August 7, 2021

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


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

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 { }.


Forum|alt.badge.img
  • Author
  • August 8, 2021

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