Javascript to record participant entry as value for embedded data | XM Community
Solved

Javascript to record participant entry as value for embedded data

  • 26 November 2020
  • 3 replies
  • 93 views

Hi,
I setup a survey question and I would like to use my participant response (a number between 0-100) as the limit for a random function (embedded data) in Qualtrics. The random function will be used to return a response in the next question for the person, e.g. "You gave away 80 points, and received 30 points in return". I need to utilize the participant response as the limit because setting rand (0:100) means that I might return values that are above the amount that the participant reported.
After going through the community posts, I understand that it is impossible to place embedded data directly in the rand to reference, since rand can only read numerals.
Is there a way to use Javascript to record the information?
Thank you very much for your help!

icon

Best answer by npetrov937 27 November 2020, 21:52

View original

3 replies

Userlevel 4
Badge +4

Sure! Just get the piped text of your participant's response, put it in quotes and then call the parseInt method around the quoted piped text - that will force it to be interpreted as an integer and you can use it in your random function.

https://www.qualtrics.com/community/discussion/comment/32079#Comment_32079Thank you so much for your help! I attempted to do this with the following code:
Qualtrics.SurveyEngine.addOnload(function()
{
// Get the embedded data as an integer
var upperlimit = parseInt("${q://QID1/ChoiceTextEntryValue}");
var multipliedlimit = upperlimit * 3;
console.log(upperlimit);
console.log(multipliedlimit);

// Use upper limit as a floor for generating a random number
   var newtransfer = Math.floor(Math.random() * (multipliedlimit));

// Save newtransfer to embedded data
Qualtrics.SurveyEngine.setEmbeddedData("newtransfer", newtransfer);
});

But the values were not shown on Qualtrics. Could it be that I messed up the syntax somewhere?


Just a short update! I managed to get it to work at last, turned out I did not paste the code in the right location to get it to load before I needed to call the embedded data. Thanks very much for your help!

Leave a Reply