Real time calculations | XM Community
Skip to main content

Hey!

I am trying to do real time calculations in a survey to display the result to the user. 

I want to display the result of a division calculation to a user based on information provided in a previous question. 

I want to divided the total area harvested by the total lint cotton to provide a value called Lint Yield. I have tried a couple JS options but haven’t managed to get it to work. (i’m a JS novice). 

the Lint yield would then be in the following question after a page break

Any advice would be much appreciated!

 

Hi @IainS 

Add an embedded data named __js_division at the beginning of your survey flow.

In the question where you enter the values add this custom javascript code :

Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
var division = parseFloat(jQuery("#QR\\~" + this.questionId + "\\~1").val()) / parseFloat(jQuery("#QR\\~" + this.questionId + "\\~2").val());
Qualtrics.SurveyEngine.setJSEmbeddedData("division", division);
});

Display the value in the next question by piping in ${e://Field/__js_division}

Change ~1 and ~2 by your actual fields’ numbers (probably 5 and 7 in your case, if you didn’t move them around or delete some after creating them).

Inspect your preview page and look for the following to find the right IDs for your question:

 


And use Math.round() or toFixed or something like that to display the number of decimals you want.


Thank you,  it works perfectly :) 


If I wanted to do a similar calculation for a different question on the survey would I need to use a different name for the embedded data, then tailor the JS to that embedded data name and then the new QID? 


Yes, it would be better to use different embedded data fields and variable names.


Leave a Reply