Creating on click, visable maths calculations within a matrix table | XM Community
Skip to main content
Hi all,



I'm trying to do maths calculations on click so that they appear in the matrix table as below using JavaScript. I also need to restrict the user to not be able to enter anything into the calculation fields. Has anyone done anything similar before and have any code as a starting point?



!



Thanks,



Rose
Hi @rose_b



Please read this post it may help you to write code



https://www.qualtrics.com/community/discussion/926/side-by-side-question-constant-sum-and-auto-complete-dropdown
Hi @rose_b!



It looks like you may be asking about custom programming/code. We actually have a Developer Corner in the community that is reserved for these types of questions. Therefore, I have gone ahead and moved your question to that section so coders and programmers are more likely to answer your question.



P.S. They might assume that you already have a basic knowledge of programming and how to integrate it into Qualtrics. If you do not, we recommend checking out W3Schools and our pages on Adding Custom CSS and Adding JavaScript to get a basic understanding!
Hello @rose_b ,



The following code will work for Matrix table-> Test Entry, short (rows and column same as the image in the question)

Also in the validation type option of the question, select "content validation" -> "number"



Paste the following code in the js(onReady) of matrix question



for(var i=4;i<=8;i++){

jQuery(".ChoiceRow:eq(3) td.c"+i+" input").prop("disabled",true);

jQuery(".ChoiceRow:eq(5) td.c"+i+" input").prop("disabled",true);

}



this.questionclick = function(event,element){

jQuery(".ChoiceRow:eq(3) td.c4 input").val(parseInt(parseInt(jQuery(".ChoiceRow:eq(1) td.c4 input").val())+parseInt(jQuery(".ChoiceRow:eq(2) td.c4 input").val()))||0);

jQuery(".ChoiceRow:eq(5) td.c4 input").val((parseInt(jQuery(".ChoiceRow:eq(4) td.c4 input").val())/parseInt(jQuery(".ChoiceRow:eq(3) td.c4 input").val()))||0);

jQuery(".ChoiceRow:eq(3) td.c5 input").val(parseInt(parseInt(jQuery(".ChoiceRow:eq(1) td.c5 input").val())+parseInt(jQuery(".ChoiceRow:eq(2) td.c5 input").val()))||0);

jQuery(".ChoiceRow:eq(5) td.c5 input").val((parseInt(jQuery(".ChoiceRow:eq(4) td.c5 input").val())/parseInt(jQuery(".ChoiceRow:eq(3) td.c5 input").val()))||0);

jQuery(".ChoiceRow:eq(3) td.c6 input").val(parseInt(parseInt(jQuery(".ChoiceRow:eq(1) td.c6 input").val())+parseInt(jQuery(".ChoiceRow:eq(2) td.c6 input").val()))||0);

jQuery(".ChoiceRow:eq(5) td.c6 input").val((parseInt(jQuery(".ChoiceRow:eq(4) td.c6 input").val())/parseInt(jQuery(".ChoiceRow:eq(3) td.c6 input").val()))||0);

jQuery(".ChoiceRow:eq(3) td.c7 input").val(parseInt(parseInt(jQuery(".ChoiceRow:eq(1) td.c7 input").val())+parseInt(jQuery(".ChoiceRow:eq(2) td.c7 input").val()))||0);

jQuery(".ChoiceRow:eq(5) td.c7 input").val((parseInt(jQuery(".ChoiceRow:eq(4) td.c7 input").val())/parseInt(jQuery(".ChoiceRow:eq(3) td.c7 input").val()))||0);

jQuery(".ChoiceRow:eq(3) td.c8 input").val(parseInt(parseInt(jQuery(".ChoiceRow:eq(1) td.c8 input").val())+parseInt(jQuery(".ChoiceRow:eq(2) td.c8 input").val()))||0);

jQuery(".ChoiceRow:eq(5) td.c8 input").val((parseInt(jQuery(".ChoiceRow:eq(4) td.c8 input").val())/parseInt(jQuery(".ChoiceRow:eq(3) td.c8 input").val()))||0);

};
Hi Rose,



This is the solution I came up with - should also take care of and NaN errors by defaulting these values to 0.



Qualtrics.SurveyEngine.addOnReady(function() {

this.questionclick = function(event,element){



var i =0; /*Setting column number for loopage*/



while (i<6){

var a = parseInt(this.getChoiceValue(2,i),10);

var b = parseInt(this.getChoiceValue(3,i),10);

/*default NaN to 0 */

if(isNaN(a)){

a= 0;

}

if(isNaN(b)){

b= 0;

}

var x = a+b ;



/*Set Value*/

this.setChoiceValue (4,i,x);



var y = parseInt(this.getChoiceValue(5,i),10)/parseInt(this.getChoiceValue(4,i),10);



if(isNaN(y)){ /*check if div by 0*/

y = 0;

}

/*Set Value*/

this.setChoiceValue (6,i,y);



/*Increase column number*/

i++;



}}

});
Thanks everyone for your help! Thank you @mattr51 - that did the job!

Leave a Reply