Javascript if statement to set embedded data | XM Community
Skip to main content

My survey is a set of questions with the answers being scored 1-5. There are breaks between each section to show the score for previous section. The score is just an average of the responses which is easy, but I do not want to show the score as a number but instead a label. I'm hoping to set an embedded data field for each section with the label based on the score using an if statement in Javascript but can't get it to work.

Here's an example of what I'm trying so far.
if("${e://Field/Score}"<=2) {Qualtrics.SurveyEngine.setEmbeddedData("Label","Average")}
else if("${e://Field/Score}"<=3){Qualtrics.SurveyEngine.setEmbeddedData("Label", "Good")}
else if("${e://Field/Score}"<=4){Qualtrics.SurveyEngine.setEmbeddedData("Label", "Great")}
else if("${e://Field/Score}"<=5){Qualtrics.SurveyEngine.setEmbeddedData("Label", "Perfect")}

You need to convert the score from a string to a number before you do the comparisons:
var score = parseInt("${e://Field/Score}");


Thanks TomG! It's now working and that embedded data field is coming through.
Issue now is that everything is coming through as Average even when they have scored higher and I realized that it's the equation for score that is not working correctly.
I currently have this javascript at the end of the page with the questions that make up the score. Does it need a page break or is it the equation that's wrong?
Qualtrics.SurveyEngine.addOnload(function()
{
Qualtrics.SurveyEngine.setEmbeddedData( "Score", '$e{ ( q://QID13/SelectedChoicesRecode + q://QID14/SelectedChoicesRecode + q://QID20/SelectedChoicesRecode + q://QID21/SelectedChoicesRecode ) / 4 }' );
 var score = parseInt("${e://Field/Score}");
if("${e://Field/Score}"<=2) {Qualtrics.SurveyEngine.setEmbeddedData("Label","Average")}
else if("${e://Field/Score}"<=3){Qualtrics.SurveyEngine.setEmbeddedData("Label", "Good")}
else if("${e://Field/Score}"<=4){Qualtrics.SurveyEngine.setEmbeddedData("Label", "Great")}
else if("${e://Field/Score}"<=5){Qualtrics.SurveyEngine.setEmbeddedData("Label", "Perfect")}
});


Pipes are resolved before the page is sent to the browser. So you can't answer a question and pipe it on the same page. You also can't set an embedded data field and pipe it on the same page.


Leave a Reply