Displaying Dynamic Sum on Current Page | XM Community
Skip to main content

Currently I have a page with multiple constant sum questions, and would like to display the sum of their totals for the survey respondent. I have looked around at a few examples and from my understanding I would need to add event listeners. Before I go off learning how to do that, I just want to make sure that is the correct method for doing this. Any insight here would be greatly appreciated, thanks!

Yes, you need to use on blur event if the question type is text entry constant sum and if it is draggable bars then use mouse up event.


I'm most likely not going about this in the best way possible, but here's what I've attempted so far. I was able to develop a table where I could input the numbers and it would automatically give me the sum
Qualtrics.SurveyEngine.addOnload(function()
{

    jQuery('#textthree').keyup(function(){
        var textone;
        var texttwo;
var textthree;
        textone = parseFloat(jQuery('#textone').val());
        texttwo = parseFloat(jQuery('#texttwo').val());
textthree = parseFloat(jQuery('#textthree').val());
        var result = textone + texttwo + textthree;
        jQuery('#result').val(result.toFixed(2));

});
});
Where my html code was

1


2


3


4


However I've realized that the data here wasn't captured in my data & analysis
I tried replicating the first code using a constant sum question, and I assume my piped values (${q://QID4/ChoiceNumericEntryValue/1}, ${q://QID4/ChoiceNumericEntryValue/2}, ${q://QID4/ChoiceNumericEntryValue/3}) would respectively replace ‘#textone’ ‘#texttwo’ ‘#textthree’ however I’m not too sure how I would format that, or if I’m even right. Apologies if this is all basic for you, I’ve kind of been thrown into this position having no prior experience with javascript.


I've tried working at it some more and have created a button that shows the sum of the two tables, however it won't work unless I go to the next page and then back to the page with the button
Qualtrics.SurveyEngine.addOnload(function()
{
var p1 = "${q://QID4/TotalSum}";
var p2 = "${q://QID10/TotalSum}";



document.getElementById("myBtn").addEventListener("click", function() {
  myFunction(p1,p2);
});


function myFunction(a,b) {
  var result = Number(a)+Number(b);
  document.getElementById("demo").innerHTML = result;
}


});

If anyone knows how to make so that clicking the button will give the total result, that'd be much appreciated.


Answered here: https://stackoverflow.com/a/61229796/4434072


Leave a Reply