
Question
Is it possible to add script to a total sum table?
I would like to take the answers that are entered in a matrix total sum table and convert the units in real time. Is this possible? For example, if the respondent answers the area of land question in hectares, this is converted to acres. If they answer in acres this is converted into hectares, and both columns sum to the total land area. (See screen shot below).
!

Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.

Qualtrics.SurveyEngine.addOnload(function()
{
var texts = jQuery("#"+this.questionId+" .InputText");
var q = jQuery("#"+this.questionId+" .QuestionBody");
var cc = jQuery("#"+this.questionId+" .ControlContainer");
var inputs = $(this.getQuestionContainer()).select('input[type="text"]');
var units1 = ["cm","kg "];
var units2 = ["in", "lb"];
var ids = ["vyska", "vaha"];
/*Add text fields for the other units*/
/*https://www.qualtrics.com/community/discussion/102/how-to-add-static-text-after-a-text-entry-box*/
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
$(input).insert({after: units1[i] + ' / <input type="text" id=' + ids[i] +' size="1" style = " text-align: right; width: 60px;">' + units2[i]});}
var vyska = document.getElementById("vyska");
var vaha = document.getElementById("vaha");
var inputsAll = [inputs[0], inputs[1], vyska, vaha];
console.log(inputsAll);
/*Allow just 3 digits or comma or dot or and one decimal digit*/
for (var i = 0; i < inputsAll.length; i++) {
setInputFilter(inputsAll[i], function(value) {
return /^\\d{0,3}[.,]?\\d{0,1}$/.test(value);});}
/*Units convertion*/
vyska.addEventListener("blur", function(){
var temp = toNum(vyska.value);
vyska.value = temp;
var res = temp*2.54;
res = round(res,1);
inputs[0].value = res});
vaha.addEventListener("blur", function(){
var temp = toNum(vaha.value);
vaha.value = temp;
var res = temp/2.20462262;
res = round(res,1);
inputs[1].value = res});
texts[0].on('blur',function(){
var temp = toNum(texts[0].value);
texts[0].value = temp;
var res = temp/2.54;
res = round(res,1);
vyska.value = res;
});
texts[1].on('blur',function(){
var temp = toNum(texts[1].value);
texts[1].value = temp;
var res = temp*2.20462262;
res = round(res,1);
vaha.value = res;
});
/*Do blur also when enter is pressed
https://stackoverflow.com/questions/16011312/execute-function-on-enter-key*/
document.addEventListener("keydown", function (e) {
if (e.keyCode === 13) {if (document.activeElement) {document.activeElement.blur();}}
});
});