Hello! I'm a complete newbie to JavaScript coding, and I need a little help modifying a code I have to serve a slightly different purpose. The code below uses the date entered for the question, adds 10 days, and then sets the new date as embedded data. (A friend helped me with this part!) I need to use the same date and add 5 days to create a different embedded data point. I reviewed several other discussions and tried modifying the code on my own, but instead of adding 5 days to the answer entered, it added 5 days to the original calculation (answer entered + 10). So . . . how would I modify this to get it to calculate Date + 10 AND Date + 5 separately (and set both as different embedded data)? (I'm also OK with setting up the Date + 5 as part of another question, just pulling the answer from the previous question . . . but I wasn't sure exactly what to change to make that work.) Thanks in advance for your help!
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
jQuery(".InputText:eq(0)").on("change", function(){
var theDate = new Date(jQuery(".InputText:eq(0)").val())
var futureDateRaw = new Date(theDate.setDate(theDate.getDate() + 10));
console.log(futureDateRaw)
var futureDate = (futureDateRaw.getMonth() + 1) + '/' + futureDateRaw.getDate() + '/' + futureDateRaw.getFullYear()
Qualtrics.SurveyEngine.setEmbeddedData("DatePlus10", futureDate);
})
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
Modifying Javascript Code to Add Days to Date Entered
Best answer by Tom_1842
Hi there, if you still need, I was able to put this in place by first creating the Embedded Data fields of "DatePlus5" and "DatePlus10" and putting them at the top of the Survey Flow. Then, on the Text Entry question that collects the date, the following can be added to the OnReady section of the question's JavaScript:
var qid = this.questionId;
jQuery("#"+qid+" .InputText:eq(0)").on("change", function() {
var theDate = new Date(jQuery("#"+qid+" .InputText:eq(0)").val());
var futureDateRaw5 = new Date(theDate);
futureDateRaw5.setDate(futureDateRaw5.getDate()+5);
var futureDateRaw10 = new Date(theDate);
futureDateRaw10.setDate(futureDateRaw10.getDate()+10);
console.log(futureDateRaw5);
console.log(futureDateRaw10);
var futureDate5 = (futureDateRaw5.getMonth() + 1) + '/' + futureDateRaw5.getDate() + '/' + futureDateRaw5.getFullYear();
var futureDate10 = (futureDateRaw10.getMonth() + 1) + '/' + futureDateRaw10.getDate() + '/' + futureDateRaw10.getFullYear();
Qualtrics.SurveyEngine.setEmbeddedData("DatePlus5", futureDate5);
Qualtrics.SurveyEngine.setEmbeddedData("DatePlus10", futureDate10);
});
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.