How do I report the dynamic calculation of a difference b/n a constant and text entry? | XM Community
Question

How do I report the dynamic calculation of a difference b/n a constant and text entry?

  • 2 April 2019
  • 4 replies
  • 40 views

I'd like to dynamically report to my survey taker the difference between a constant and a value they are entering into a textbox.

I'm asking them to report a desired salary into a textbox (e.g., 100000) and I'd like to report to them the difference between that entered value and a constant (e.g., 200000) in a dynamic fashion. Essentially, this would be similar to the Constant Sum function (with the dynamic updated as values are entered) but resulting from the difference between my constant and the value they are entering into the textbox.

I'm a Javascript novice, so I'm not sure how to tease it apart.

4 replies

Userlevel 7
Badge +11
You can do this with embedded data and math operations.

You'll want one embedded data field for your constant, declared at the beginning of your survey flow, then one to capture the input from your text field. Then use math operations to calculate the difference as a third embedded data field and you can report it out with piped text. Be sure to save the embedded data as number types, too!
Thank you for this. It's a big help. However, I can only get it to display the calculated value once the answer in the text question is entered (i.e., by clicking next). So, the calculated value displays on the next page.

This is will if I have to, but is there a way to get it to dynamically update on the same page? For example, as they enter a value in the textbox, they can see a total updated in real time?

Like with this expression: $200000 - {value entered into textbox}.
Userlevel 6
Badge +27

Hi! for the operation in the embedded data to work you will need it to set it on a new block or page. This happens because of how the survey flow works that you will need to complete the previous block to move to the embeded data block

Userlevel 6
Badge +21

Hi ,
I have the below code for you which calculates the value using javascript and show the results in same page.
here is the form text entry question with two textbox
image.pngand below is the code which is added in the question javascript.
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
  jQuery('input[type="text"]').eq(1).attr("disabled", "disabled"); //disabled the editing of textbox in which difference value is shown
jQuery("input").change(function(){
var value1 = jQuery('input[type="text"]').eq(0).val(); //capturing the entered text
var constant = 100; //change the constant value here
var diff = constant - value1; //calculating difference between entered and constant value
jQuery('input[type="text"]').eq(1).val(diff); //showing the value in text box.
});

});

Leave a Reply