Matrix Profile question - count number of selected cells per column | XM Community
Skip to main content
Hi everyone,

I am trying to create a profile-type Matrix question, from which I want to:

1. count the total number of selections in each column of the Matrix, and assign a different value to each column (e.g. col 1 is worth 1 point, col 2 is worth 2 points, and col 3 is worth 3 points),

2. count the total of each column-total

3. save a grand total, making sure this grand total remains below an embedded data field, which is saved from a previous answer.



The final purpose of this question is to ask respondent to tell me how much they spend monthly across:

- 5 categories of expenditures, set up in 5 rows.

- 3 profiles, set up in 3 columns.



Can you help me out? I am new to Javascript, so please give me a detailed answer or even a snippet of code, that would greatly help!
So far, I've been able to save as embedded data the selection value for each row, upon clicking on a cell of a given row.



Now the main issue is to save the grand total. Currently, what my code relative to the grand total is doing is the following: every time I click on a cell, it concatenates the column number of the selected cell to the current value of the grand total ("totalRow").

For example, if I click on this sequence:

- row 1, column 1,

- row 2, column 3

- row 3, column 1

- row 4, column 2

- row 5, column 1,

- then re-click on row 5, now column 3,

what is saved as embedded data field "totalRow" is: "131213".



Does anybody know how to save instead the actual sum of the final selections, i.e. 10 ?



Thanks in advance!



Here's my current code:









Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/



var qid = this.questionId;

var totalRow = 0;



this.questionclick = function(event,element){

// Saving all answers for each of 5 set categories.

var row1 = 0;

var row2 = 0;

var row3 = 0;

var row4 =0;

var row5 =0;



if (element.type == 'radio'){

// This reads the column and the row of a currently selected button. [1] is the qid.

var colNum = element.id.split('~')[3];

var rowNum = element.id.split('~')[2];

alert('You clicked on choice: column '+colNum+' of row '+rowNum);



if (rowNum == 1){

if (colNum== 1){

row1 = row1 +1;

}else if (colNum== 2){

row1 = row1 +2;

}else if (colNum== 3){

row1 = row1 +3;

}

Qualtrics.SurveyEngine.setEmbeddedData( 'row1', row1 );



}else if (rowNum == 2){

if (colNum== 1){

row2 = row2 +1;

}else if (colNum== 2){

row2 = row2 +2;

}else if (colNum== 3){

row2 = row2 +3;

}

Qualtrics.SurveyEngine.setEmbeddedData( 'row2', row2 );



}else if (rowNum == 3){

if (colNum== 1){

row3 = row3 +1;

}else if (colNum== 2){

row3 = row3 +2;

}else if (colNum== 3){

row3 = row3 +3;

}

Qualtrics.SurveyEngine.setEmbeddedData( 'row3', row3);



}else if (rowNum == 4){

if (colNum== 1){

row4 = row4 +1;

}else if (colNum== 2){

row4 = row4 +2;

}else if (colNum== 3){

row4 = row4 +3;

}

Qualtrics.SurveyEngine.setEmbeddedData( 'row4', row4);



}else if (rowNum == 5){

if (colNum== 1){

row5 = row5 +1;

}else if (colNum== 2){

row5 = row5 +2;

}else if (colNum== 3){

row5 = row5 +3;

}

Qualtrics.SurveyEngine.setEmbeddedData( 'row5', row5);

}



totalRow = parseInt(totalRow) + colNum;

Qualtrics.SurveyEngine.setEmbeddedData( 'totalRow', totalRow);



} // end if



} // end this.questionclick



});
I've managed a very inelegant way to get my Total over all rows and columns using addOnPageSubmit. But still, I can't use my total in my onClick part (previous comment), as I only managed to calculate my total on another section of code (addOnPageSubmit).



Here is the extra chunck of code. Please help me improve this very yucky code!!





Qualtrics.SurveyEngine.addOnPageSubmit(function()

{

/*Place your JavaScript here to run when the page is submitted */



var qid = this.questionId;

var totalRow = 0;



for ( rowNum = 1; rowNum <= 5; rowNum++){

for ( colNum = 1; colNum <= 3; colNum++){

var resp = Qualtrics.SurveyEngine.registry[qid].getChoiceValue(rowNum, colNum); // boolean: 1 if selected, false otherwise.

if (resp == true){

resp = colNum;

}else{

resp = 0;

}

totalRow = totalRow + resp;

}

}



Qualtrics.SurveyEngine.setEmbeddedData( 'totalRow', totalRow);







});
Hey @AdeGendre! Although I do recognize you are asking for a Custom Code solution, I believe you may be able accomplish part of what you explained with the use of Scoring and Math Operations. Be sure to check out those pages! If you have any additional questions, feel free to reach out to our Support Team!
@AdeGendre,



Can you post an image of your question? Your code looks like you are just using a Likert Matrix with radio buttons, so I don't understand what you mean by profile,

Leave a Reply