When does the JS code load and run. | XM Community
Skip to main content
In my RC survey I have a need to display a username in a question and I need to parse it out of the recipients email address. I did not see any way to do this in standard Q so I decided to use JS to do it. Here is my code.



```

var str = "${m://Email1}";

var substrings = str.split("@",2);

console.log (str);

console.log (substrings.length);

console.log (substrings[0]);

console.log (substrings[1]);

Qualtrics.SurveyEngine.setEmbeddedData(\\"userID\\",substrings[0]);



```

The code runs and does what I expect expect I can not get it to change the value of the "userID" field if I add the code to the same block where the variable is used. I have tried to put it in the Qualtrics.SurveyEngine.addOnload and I see it execute in the console log but the value displayed in the question is not the value passed in by the JS code. The only way I can get it to work properly is to load the code on a prior block in the survey.



Questions:

Is there an easier way to parse this string inside the survey with using the JS?

Am I doing something wrong in placing this code to make it execute before the page displays?

Is there a place I can look for a middle level primer on the Qualtrics / JS interaction. Everything I have seen is either very basic or very advanced.



Many Thanks!!
@SteveS,



Pipes and embedded data are resolved and set on the server. So, you can't parse, set, then pipe the embedded data you just set on the same page.



So let's say you want to parse the userID from the email, display it, and save it for later. You would have html in your question that looked something like:

```

<span id="userID"><span>

```

Then your JS would be something like:

```

Qualtrics.SurveyEngine.addOnload(function() {

var substrings = "${m://Email1}".split("@",2);

jQuery("#userID).html(substrings[0]);

Qualtrics.SurveyEngine.setEmbeddedData("userID",substrings[0]);

});

```
Thanks Tom. This makes more sense now that I understand the flow.
Thanks again! This worked perfectly There was only 1 small syntax error (missing the " after the userID in the JQuery) I am re-posting here with that included.



```Qualtrics.SurveyEngine.addOnload(function() {

var substrings = "${m://Email1}".split("@",2);

jQuery("#userID").html(substrings[0]);

Qualtrics.SurveyEngine.setEmbeddedData("userID",substrings[0]);

});

```

Leave a Reply