How to do math when question is the last one in your survey? | XM Community
Skip to main content

Hi all. There have been some great resources on this forum for us non-coders to be able to do date math. Thank you!
I am working to create a mini-database in Qualtrics. The flow of the form means the last field I enter is a date. However, that field needs used to calculate a time span between it an an earlier entered date.
All of the examples I have seen put Javascript on a question on the next page in the Onload function. In my case I don't have a next page and question. Is there somewhere else to put this or a way to force the calculation while on the same page as the question? It doesn't make sense for the data entry clerks to have to navigate through an extra page every time they go to enter a case.

Assuming you are using JS to do the date calculation and storing it in an embedded data field, you can use the addOnPageSubmit() function. You'll need to get the date from the current page directly from the input field rather than pipe it.


TomG I am close but not quite there. If you can help me get over this hurdle that would be great.
I am using flatpicker, I've loaded all the links for that and jsmoment in my survey header.
For the first segment of my survey, I need to do math between dates that are entered in two different questions on one screen. I am finding that I need to have my Javascript on all one question to make it work, which means I need to be able to hard code one of the two dates and not use this.questionId. I've been doing this in steps instead of one easy line of code to check that I am reading in my variables correctly (and QID5 is coming in properly!). Right now I have the following Javascript on QID5...
//
Qualtrics.SurveyEngine.addOnPageSubmit(function(){
var textDateID5 = moment(jQuery("#"+this.questionId+" .InputText").val());
Qualtrics.SurveyEngine.setEmbeddedData("ID5date", textDateID5);
var textDateID4 = moment(jQuery("#QR~QID4. InputText").val());
Qualtrics.SurveyEngine.setEmbeddedData("ID4date", textDateID4);
var textidage = moment(textDateID5).diff(moment(textDateID4),'years');
  Qualtrics.SurveyEngine.setEmbeddedData("textIDage", textidage);
});
//
From my testing I know that I am breaking/erroring out on pulling in the value for QID4. Any help you can give me on reading in a value without using this.questionId is greatly appreciated.
Ultimately I have to calculate age and differences in days depending on dates I am comparing. I am sure I can do that by changing the second to last line to end with 'days' instead of 'years'. But reading in the value for QID4 is the last bump I anticipate running in to and need help with.
Thank you!


https://community.qualtrics.com/XMcommunity/discussion/comment/47502#Comment_47502You don't need to hardcode the previous question. You can do this:
var q = jQuery("#"+this.questionId); //the current question
var pq = q.prevAll(".QuestionOuter").first(); // the previous question
var textDateID4 = moment(pq.find(".InputText").val());
var textDateID5 = moment(q.find(".InputText").val());


Leave a Reply