How do I get the value of embedded data with javascript? | XM Community
Question

How do I get the value of embedded data with javascript?

  • 6 April 2021
  • 2 replies
  • 1410 views

I am trying to implement a timer so that after a given time the user is automatically moved to the next block.
This questions is literally EXACTLY what I am trying to implement: https://www.qualtrics.com/community/discussion/comment/5616#Comment_5616
In that question, someone said I should create embedded data before my block with a value of 180.
Then, apparently, the following code should automatically move the user to the end of the block after 180 seconds.
var timeleft = parseInt("${e://Field/TimeLeft})");

var timer = setInterval(function () {
  if (timeleft <= 0) {
    clearInteval(timer);
    $('NextButton').click();
  }
  timeleft--;
}, 1000);

$('NextButton').onclick = function (event) {
  Qualtrics.SurveyEngine.setEmbeddedData('TimeLeft', timeleft);
}
However, whatever I try to do the, when I try to log the value of TimeLeft, nothing gets logged to the console.
I added an image how I am currently adding the embedded data before my block.
embedded-data.pngWhat am I doing wrong here?


2 replies

Userlevel 2
Badge +10

Hi there,
Your code/process seemed to work out for me in terms of logging the time. The only difference is that I had to add the following code to my JS at the bottom:
Qualtrics.SurveyEngine.addOnUnload(function()
{

timer = clearInterval(); 

});
This is, of course, if "logged to the console" means logged as an embedded data field. Otherwise, be sure to add console.log(timeleft) in various places throughout your code above. For example, I tested with:
Qualtrics.SurveyEngine.addOnload(function()
{
var timeleft = parseInt("${e://Field/TimeLeft})");
console.log(timeleft);


var timer = setInterval(function () {
  if (timeleft <= 0) {
    clearInteval(timer);
    $('NextButton').click();
  }
  timeleft--;
}, 1000);


$('NextButton').onclick = function (event) {
  Qualtrics.SurveyEngine.setEmbeddedData('TimeLeft', timeleft);
  console.log(timeleft); 
}


});
In terms of moving to the end of the block, this code won't automatically do that. Are you trying to do this just for one questions, or over multiple questions?

Userlevel 1
Badge +1

Hi there,
Your code/process seemed to work out for me in terms of logging the time. The only difference is that I had to add the following code to my JS at the bottom:
Qualtrics.SurveyEngine.addOnUnload(function()
{

timer = clearInterval(); 

});
This is, of course, if "logged to the console" means logged as an embedded data field. Otherwise, be sure to add console.log(timeleft) in various places throughout your code above. For example, I tested with:
Qualtrics.SurveyEngine.addOnload(function()
{
var timeleft = parseInt("${e://Field/TimeLeft})");
console.log(timeleft);


var timer = setInterval(function () {
  if (timeleft <= 0) {
    clearInteval(timer);
    $('NextButton').click();
  }
  timeleft--;
}, 1000);


$('NextButton').onclick = function (event) {
  Qualtrics.SurveyEngine.setEmbeddedData('TimeLeft', timeleft);
  console.log(timeleft); 
}


});
In terms of moving to the end of the block, this code won't automatically do that. Are you trying to do this just for one questions, or over multiple questions?

I’m trying to have the yes/no response recorded in addition to the time. The time has been working fine but the yes/no responses were not being recorded. I have  60 questions that I need the data recorded. I know I will need to adjust the JS for each question and I suspect I will need to add an embed block before each one as well.

Leave a Reply