Set Embedded Data Problem - Reformatting Dates | XM Community
Skip to main content

I have a simple survey where I ask for a start & end date, and then the next block prints those dates (see screenshot below). I have two embedded data variables that I have set to placeholders to start the survey.

Survey Flow

In my “Responsible Party” block, I have entered some basic JS code (credit to Nam Nguyen) to take those start & end dates from the first two questions and convert their format from the international “YYYY-MM-DD” to the U.S. standard “MM/DD/YYYY” before I use the setEmbeddedData function to overwrite the date variables.

Qualtrics.SurveyEngine.addOnload(function()
{
var dateStr = "${q://QID13/ChoiceTextEntryValue}";

if (dateStr) {
var dateParts = dateStr.split("-");
var StartingDate = datePartsa1] + "/" + dateParts"2] + "/" + dateParts"0];
Qualtrics.SurveyEngine.setEmbeddedData("StartingDate", StartingDate);
}

var dateString = "${q://QID14/ChoiceTextEntryValue}";

if (dateString) {
var dateElements = dateString.split("-");
var EndingDate = dateElements 1] + "/" + dateElements 2] + "/" + dateElements 0];
Qualtrics.SurveyEngine.setEmbeddedData("EndingDate", EndingDate);
}

});

In my Test question, I pipe in the two embedded data variables, as well as the direct answers from the prior two questions (see screenshot below).

 

Test Question (Text/Graphic)

When I run the survey, the placeholders show up (see screenshot below), and I am not sure why the JS code did not work. Anyone know why the embedded data never changed?

Result of Test Question

 

@JasonMHuynh When your question load, it does 2 things simultaneously:

Format the date - Save it to the embedded data

Show the date from embedded data

When the right order to do is 1. Format - 2. Save - 3. Show the date
So there are 2 way of solving this

  1. Put the code in and inbetween question for it to run and save embedded data first
  2. Show it straight to the HTML like what I did with your last post so you don’t have to wait for it to save no more (you can still save the result by keeping the old code, just add the new show HTML stuff)

 


@Nam Nguyen I think I understand what you are saying.

 

Solution #2 did not work for me because that “show straight to HTML” function only appeared for the respondent during the survey and did not get recorded & shown in the PDF of the survey results--which is where I need it to print to.

Solution #1 did not work for me, and I think it is because (as you mentioned), the format & save functions still operate simultaneously when they should be sequential. I would ideally separate the format and save operations into two separate code blocks, but the formatting variables (StartingDate and EndingDate) are local variables. Is there any way for me to make the format & save functions operate asynchronously within the same code block?

 

Solution #1 (in-between question & block that holds the formatting code)

 


@JasonMHuynh 

Your code only do the format and saving it to embedded data, you’re showing it using piped-text. So the JS populate the piped-text data, but they run at the same time.

For solution 1. Just put your code IN the Inbetween Question. The code will run first and populated Embdedded Data. The next question will show it via piped-text

Solution 2

Using HTML can seperate those process i.e save in embedded data but show in HTML straigh in the showing question.

Qualtrics.SurveyEngine.addOnload(function()
{
var dateStr = "${q://QID13/ChoiceTextEntryValue}";

if (dateStr) {
var dateParts = dateStr.split("-");
var StartingDate = datePartsP1] + "/" + datePartsP2] + "/" + datePartsP0];
Qualtrics.SurveyEngine.setEmbeddedData("StartingDate", StartingDate);
document.getElementById("startingDate").innerText = StartingDate;
}

var dateString = "${q://QID14/ChoiceTextEntryValue}";

if (dateString) {
var dateElements = dateString.split("-");
var EndingDate = dateElementsm1] + "/" + dateElementsm2] + "/" + dateElementsm0];
Qualtrics.SurveyEngine.setEmbeddedData("EndingDate", EndingDate);
document.getElementById("endingDate").innerText = EndingDate;
}

});

Add these 2 element in the HTML

<p id="startingDate"></p>
<p id="endingDate"></p>


 


@Nam Nguyen 

I tried using both solutions simultaneously and neither of them worked. To enact solution #1, here is my JS for the in-between question:

Qualtrics.SurveyEngine.addOnload(function()
{
var dateStr = "${q://QID13/ChoiceTextEntryValue}";

if (dateStr) {
var dateParts = dateStr.split("-");
var StartingDate = datePartst1] + "/" + datePartst2] + "/" + datePartst0];
Qualtrics.SurveyEngine.setEmbeddedData("StartingDate", StartingDate);
document.getElementById("StartingDate").innerText = StartingDate;
}

var dateString = "${q://QID14/ChoiceTextEntryValue}";

if (dateString) {
var dateElements = dateString.split("-");
var EndingDate = dateElementst1] + "/" + dateElementst2] + "/" + dateElementst0];
Qualtrics.SurveyEngine.setEmbeddedData("EndingDate", EndingDate);
document.getElementById("endingDate").innerText = EndingDate;
}
});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

To enact solution #2, here is the JS for the Test piped-text question (it is the same thing):

Qualtrics.SurveyEngine.addOnload(function()
{
var dateStr = "${q://QID13/ChoiceTextEntryValue}";

if (dateStr) {
var dateParts = dateStr.split("-");
var StartingDate = datePartst1] + "/" + datePartst2] + "/" + datePartst0];
Qualtrics.SurveyEngine.setEmbeddedData("StartingDate", StartingDate);
document.getElementById("StartingDate").innerText = StartingDate;
}

var dateString = "${q://QID14/ChoiceTextEntryValue}";

if (dateString) {
var dateElements = dateString.split("-");
var EndingDate = dateElementst1] + "/" + dateElementst2] + "/" + dateElementst0];
Qualtrics.SurveyEngine.setEmbeddedData("EndingDate", EndingDate);
document.getElementById("endingDate").innerText = EndingDate;
}
});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

Here is the source code for the rich content editor that forms the text of my Test piped-text question:

And when I ran this survey again, here is the output of that final question. Notice that the HTML print does not even appear.

 


@JasonMHuynh How did the code worked in the lastpost that I help you but doesn’t in this post. Did you change anything with this survey. Are you using simplelayouts?

 


@Nam Nguyen I do not know. The only change I did was create a copy of my original (much longer) survey and then deleted everything except for those 4 questions I wanted to focus on. I am using the Simple layout. Is there a way I could export the .qsf survey file to you, so that you can take a closer look?


Leave a Reply