Is it possible to add script to a total sum table? | XM Community
Question

Is it possible to add script to a total sum table?

  • 9 April 2019
  • 5 replies
  • 10 views

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).
!

5 replies

Userlevel 5
Badge +6
Hi @jill_caviglia_harris,
I think it is possible, but it will be quite time demanding to implement it and I think no one including have time to do it.
Nevertheless I have done something similar for my last survey. I asked for body height and width and enabled respondents to answer always in two different units. The answer was converted to the other units after the field with first units lost focus.
Here you find my question and code for inspiration. You could either add there some custom fields with sums or re-implement the code for you question type.

!


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();}}
});

});
Hi @fleb. than for sharing your code. This is not exactly what I am looking for (I'd like the respondents to see the conversions) but is a good start.
> @jill_caviglia_harris said:
> Hi @fleb. than for sharing your code. This is not exactly what I am looking for (I'd like the respondents to see the conversions) but is a good start.

For this, you cannot show that in the total box but you can add custom HTML element and show the conversion there dynamically on change event of text box.
Userlevel 5
Badge +6
> @jill_caviglia_harris said:
> Hi @fleb. than for sharing your code. This is not exactly what I am looking for (I'd like the respondents to see the conversions) but is a good start.

Respondents will see conversions. They're done when user clicks outside current element. If you want to see the conversion immediately when user presses or releases a key, just replace `blur` with `keyup` or `keydown`.
Great. Thanks.

Leave a Reply