Creating Calculation in Matrix Table Column | XM Community
Skip to main content

Creating Calculation in Matrix Table Column

  • March 2, 2023
  • 1 reply
  • 114 views

Hello! I am trying to do something that seems a bit complicated.
Basically have a matrix table question and I'm trying to insert a column which automatically calculates and displays a number, based on the values inserted in other columns. Just as an example, column 5 would display the math operation (column 3/column 4).
Could someone help me do the coding for this, especially when I need the table to be formatted according to carry forwarded items on the left hand side of the table?
Screenshot 2023-03-02 174834.jpg
Thanks!

1 reply

qualtrics_nerd
Level 5 ●●●●●
Forum|alt.badge.img+19
  • Level 5 ●●●●●
  • 225 replies
  • April 20, 2023

Hi @PhilippeB ,

Assuming that you are keeping the structure as above adn there no other questions present on the page, you can achieve the above functionality, by using below code:
 

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*/

function calc() {


const box_1 = document.querySelectorAll('input[id*="~3"][type="text"]');
const box_2 = document.querySelectorAll('input[id*="~4"][type="text"]');
const mos = document.querySelectorAll('input[id*="~5"][type="text"]');

for (let i = 0; i < mos.length; i++) {
    mos[i].disabled=true;
  const value1 = parseInt(box_1[i].value);
  const value2 = parseInt(box_2[i].value);
  if (isNaN(value1) || isNaN(value2)) {
    mos[i].value = 0;
  } else {
    mos[i].value = value1 / value2;
  }
}
}

document.getElementById("Questions").addEventListener("input", calc);


});

Qualtrics.SurveyEngine.addOnUnload(function()
{

});

Hope this resolves your query😊!!


Leave a Reply