"If I ask people to enter a date, say the day that their assignment is due, how can I then generate the date two weeks earlier?"
```
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var startDate = moment(jQuery("#"+this.questionId+" .InputText").val()).subtract(2, 'weeks');
Qualtrics.SurveyEngine.setEmbeddedData('startDate', startDate);
});
```
Can moment.js work on addOnReady? I tried the following setup to set an embedded value of an age calculation when you off focus, however, it's not working. I only am able to get it to work on page submit in your example above:
var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText").val()), 'years');
jQuery("#"+this.questionId+" .InputText.QR-QID1").focusout(function(){
Qualtrics.SurveyEngine.setEmbeddedData('age', age);
console.log(age);
});
Any thoughts on calculating before the page submits?
Your age calculation has to be inside the focusout event handler.
Even if I place inside like this under .addOnReady, the console is printing 0:
jQuery("#"+this.questionId+" .InputText.QR-QID1").focusout(function(){
var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText.QR-QID1").val()), 'years');
Qualtrics.SurveyEngine.setEmbeddedData('age', age);
console.log(age);
Thoughts?
quincyadam
Post your entire script as a Code Block (click on the paragraph mark to the left, then ")
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
// This is not calculating the age. It returns 0
jQuery("#"+this.questionId+" .InputText.QR-QID1").focusout(function(){
var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText.QR-QID1").val()), 'years');
Qualtrics.SurveyEngine.setEmbeddedData('age', age);
console.log(age);
// This is writing the DOB to console
var dob = jQuery(".InputText.QR-QID1").val();
console.log(dob);
// This is actually calculating the age, but posting a depraction warning: moment.js:293 Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
var newage = moment().diff(moment(jQuery(".InputText.QR-QID1").val()), 'years');
console.log(newage);
});
quincyadam
A few things:
- The focusout handler isn't working inside the function because 'this' is the input, not the question object. Find the input element once at the beginning and remove the unnecessary QR-QID1 class: 'var input = jQuery("#"+this.questionId+" .InputText");' then use 'input' throughout.
- You have syntax error...missing }); at end of focusout handler.
- I think the deprecation warning is because you haven't specified a date format.
Thanks TomG. I fixed 1 + 2. How/where do you include date format? I tried adding to the variable, but misunderstand where it's supposed to be applied. For example here's the updated code:
var input = jQuery("#"+this.questionId+" .InputText");
input.focusout(function(){
var age = moment().diff(moment(input.val()), 'years');
console.log(age);
});
I've tried the following:
moment().format('MM-DD-YYYY');
and
var age = moment().format('MM-DD-YYYY').diff(moment(input.val()), 'years');
and
var age = moment().diff(moment(input.val()), 'years').format('MM-DD-YYYY');
All still produce that error.
moment().format("MM-DD-YYYY") outputs a moment date in the given format. You want to parse an input string with a specific format, which is moment(input.val(),"MM-DD-YYYY");
Thanks TomG.
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.