
Solved
Side-by-side question: constant sum and auto-complete dropdown
Hi all
Hoping someone on here can help me out (or point me in the right direction!).
There are two main things that I am trying to achieve for the same question:
1. Constant sum for a column in a side-by-side question (I found some script for a matrix table but not for side-by-side). I have managed to get % signs beside the boxes but not the constant sum element. What I want to do is add a new row at the bottom and only show the cell for column 'Percentage Time Allocation on this Process Step (%)', and have that cell be the constant sum to 100%.
2. In the same question, for the column 'Standardised Activity Tag', I would like to auto-complete the dropdown menu with an embedded data field ($e{://Field/p1ps1sat for row 1 etc.). I want to give participants a pre-filled cell and allow them to change the input based on the values in the dropdown.
I don't know any JS and am scrambling to solve the above issues! If anyone could help me out, that would be very much appreciated.
Aaron!

Best answer by Samarth
Hi @aaron ,
I am getting error while uploading the QSF file please find javav script code need to be placed in
question.
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
});
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var $jq = jQuery.noConflict();// always keep this
$jq('input[type="text"]').after('%'); //insert % sign after boxes
$jq('.SBS1 select:last').hide(); // remove last select for Column 1
$jq('.SBS3 select:last').hide(); // remove last select for Column 3
$jq('.SBS4 select:last').hide(); // remove last select for Column 4
var $sum = 0; // define sum variable
$jq('.SBS2 input:last').val($sum); // setting the value of total box
//function for changing text value to numeric
function getVal(val){ var num = parseInt(val); if(isNaN(num)) { num = 0; } return num; };
// calculating some on change
$jq('.SBS2 input').not(':last').change(function(){
// for every box
var $sum = 0; // define sum variable
$jq('.SBS2 input').not(':last').each(function(){
$sum += +getVal($jq(this).val());
});
$jq('.SBS2 input:last').val($sum);
});
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
Regards,
Samarth
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.

Please refer attached QSF file with the example question and let me know if it works.
Could you please explain your second point, if you are looking for predefined values then it can easily be achieved by using "Add Default Choices" functionality.
!
Regards,
Samarth
Please use the updated code, Not sure why i am unable to upload QSF file!!
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
});
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var $jq = jQuery.noConflict();// always keep this
$jq('input[type="text"]').after('%'); //insert % sign after boxes
$jq('.SBS1 select:last').hide(); // remove last select for Column 1
$jq('.SBS3 select:last').hide(); // remove last select for Column 3
$jq('.SBS4 select:last').hide(); // remove last select for Column 4
var $sum = 0; // define sum variable
$jq('.SBS2 input:last').val($sum); // setting the value of total box
$jq('.SBS2 input:last').css('fontWeight','bold') // setting fontface="bold"
//color change
if($sum==100){
$jq('.SBS2 input:last').css('color','green');
}
else{
$jq('.SBS2 input:last').css('color','red');
}
// setting the value 0 for all boxes if blank or 0
$jq('.SBS2 input').each(function(){
$jq(this).val(getVal($jq(this).val()));
});
$jq('.SBS2 input').change(function(){
$jq('.SBS2 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 change
$jq('.SBS2 input').not(':last').change(function(){
// for every box
var $sum = 0; // define sum variable
$jq('.SBS2 input').not(':last').each(function(){
$sum += +getVal($jq(this).val());
});
$jq('.SBS2 input:last').val($sum);
//color change
if($sum==100){
$jq('.SBS2 input:last').css('color','green');
}
else{
$jq('.SBS2 input:last').css('color','red');
}
});
// default choice magic
var $embedded = [];
var $length = $jq(".SBS1 select").length;
for (var i=0;i<$length-1;i++)
{
$embedded[i] = $jq(".Choice .c1").eq(i).text().trim();
$jq(".SBS1 select").eq(i).find('option:contains(' +$embedded[i]+ ')').attr('selected','selected');
}
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
Regards,
Samarth