Side by Side column that calculates row totals | XM Community
Skip to main content
I am creating a Side by Side table that needs to have row and column totals. So far, I have been able to use code to create column totals, but I still need help with creating the row totals.



Here is the sample JavaScript code I used to create column totals (adopted from another Qualtrics user):



Qualtrics.SurveyEngine.addOnReady(function()

{

var $jq = jQuery.noConflict();// always keep this



$jq('.SBS1 input[type="text"]').before('$'); //insert $ sign before boxes

$jq('.SBS1 input[type="text"]').after('.00'); //insert .00 after boxes



var $sum = 0; // define sum variable

$jq('.SBS1 input:last').val($sum); // setting the value of total box

$jq('.SBS1 input:last').css('fontWeight','bold') // setting fontface="bold"

//color change

if($sum>0){

$jq('.SBS1 input:last').css('color','green');

}

else{

$jq('.SBS1 input:last').css('color','red');

}



// setting the value 0 for all boxes if blank or 0

$jq('.SBS1 input').each(function(){

$jq(this).val(getVal($jq(this).val()));

});



$jq('.SBS1 input').keyup(function(){

$jq('.SBS1 input').each(function(){

$jq(this).val(getVal($jq(this).val()));

});

});



//function for changing text value to numeric



function getVal(val){ var num = parseInt(val); if(isNaN(num)) { num = 0; } return num; };



// calculating some on keyup

$jq('.SBS1 input').not(':last').keyup(function(){

// for every box

var $sum = 0; // define sum variable

$jq('.SBS1 input').not(':last').each(function(){

$sum += +getVal($jq(this).val());

});

$jq('.SBS1 input:last').val($sum);

//color change

if($sum>0){

$jq('.SBS1 input:last').css('color','green');

}

else{

$jq('.SBS1 input:last').css('color','red');

}

});



// default choice magic

var $embedded = [];

var $length = $jq(".SBS2 select").length;

for (var i=0;i<$length-1;i++)

{

$embedded[i] = $jq(".SBS1 input").eq(i).val().trim();

$jq(".SBS2 select").eq(i).find('option:contains(' +$embedded[i]+ ')').attr('selected','selected');

}



});



Can I use similar code to allow a side by side column to sum row totals? How would I be able to do this?
Be the first to reply!

Leave a Reply