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

Displaying Dynamic Sum on Current Page

  • Apr 14, 2020 4:36 PM UTC
  • 4 replies
  • 177 views

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!

4 replies

rondev
Level 6 ●●●●●●
Forum|alt.badge.img+22
  • Level 6 ●●●●●●
  • Apr 14, 2020 4:41 PM UTC

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.


  • Author
  • Apr 14, 2020 10:12 PM UTC

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.


  • Author
  • Apr 14, 2020 11:36 PM UTC

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.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Apr 15, 2020 7:48 PM UTC