Writing Script to create running totals of sub-columns in Side by Side Chart | XM Community
Skip to main content

Hey, All
I am trying to create a row at the bottom of my side by side table that calculates the sum of each sub-column. Below is image of the side by side table.
qulatrics question.PNGUsers will be putting in a number under each sub column in the Race/Ethnicity Column. I want the final row to calculate a total of that column. I am a novice at Java and have been using the following code:
Qualtrics.SurveyEngine.addOnReady(function()
{
var $jq = jQuery.noConflict();// always keep this

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

$jq('.SBS1 select:last').hide(); // remove last select for Column 1
$jq('.SBS2 select:last').hide(); // remove last select for Column 2
$jq('.SBS3 select:last').hide(); // remove last select for Column 4
$jq('.SBS5 select:last').hide();
$jq('.SBS6 select:last').hide();
$jq('.SBS7 select:last').hide();

var $sum = 0; // define sum variable
$jq('.SBS4.c1 input:last').val($sum); // setting the value of total box
$jq('.SBS4.c1 input:last').css('fontWeight','bold') // setting fontface="bold"
//color change
if($sum>0){
$jq('.SBS4.c1 input:last').css('color','green');
}
else{
$jq('.SBS4.c1 input:last').css('color','red');
}

// setting the value 0 for all boxes if blank or 0
$jq('.SBS4.c1 input').each(function(){
$jq(this).val(getVal($jq(this).val()));
});

$jq('.SBS4.c1 input').keyup(function(){
$jq('.SBS4.c1 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('.SBS4.c1 input').not(':last').keyup(function(){
// for every box
var $sum = 0; // define sum variable
$jq('.SBS4.c1 input').not(':last').each(function(){
$sum += +getVal($jq(this).val());
});
$jq('.SBS4.c1 input:last').val($sum);
//color change
if($sum>0){
$jq('.SBS4.c1 input:last').css('color','green');
}
else{
$jq('.SBS4.c1 input:last').css('color','red');
}
});


});

I can get it to total all of the sub_columns and the total appears under "White", but I am not sure how to translate that each sub column

Be the first to reply!

Leave a Reply