Matrix constant sum validation | XM Community
Skip to main content

I have a constant sum matrix with three column.

I want to set a validation so that the sum of the 1st column may or may not be 100.

But the column 2 and 3 must be equal to 100.

You could use a side by side question to add the validations.

 

 

 

And then use the JS below to create the constant sum experience.

 

Qualtrics.SurveyEngine.addOnReady(function () {
const quest = this;
const qc = quest.getQuestionContainer();
const allCells = Array.from(qc.querySelectorAll(".AnswerCell"));
const allCols = s];
allCells
.map((item) =>
Array.from(item.classList).filter((a) => {
if (a.includes("SBS") || a.includes("AnswerCell")) {
return false;
} else {
return a;
}
})
)
.flatten()
.forEach((a) => {
if (allCols.indexOf(a) == -1) {
allCols.push(a);
}
});

const sumClass = allCols.pop();
const selectors = allCols.map((a) => "." + a + " input");

const allRows = Array.from(qc.querySelectorAll("tr.Choice"));

allRows.forEach((row) => {
const answerCell = row.querySelector("." + sumClass + " input");
answerCell.disabled = true;
answerCell.value = 0;

selectors.forEach((col) => {
const input = row.querySelector(col);
input.type = "number";
input.oninput = function () {
updateSum(row);
};
});
});

const updateSum = function (row) {
const allVals = selectors.map((item) => row.querySelector(item).value).map((item) => Number(item) || 0);

const answerCell = row.querySelector("." + sumClass + " input");
answerCell.value = allVals.reduce((a, b) => a + b);
};
});

 

Demo: https://iima.au1.qualtrics.com/jfe/preview/previewId/00a1841e-33a5-49ae-a9c5-6934da853af4/SV_8HBDY4VRJnFn40J/BL_0Mt1Jw9T1Jby9eu?Q_SurveyVersionID=current

 


I have a constant sum matrix with three column.

I want to set a validation so that the sum of the 1st column may or may not be 100.

But the column 2 and 3 must be equal to 100.

You can do this with two custom validation conditions, math expressions and piped values. Statement 1-Scale Point 2 should be equal to 100 minus all the other statements for scale point 2 AND Statement 1/Scale Point 3 should be equal to 100 minus all the other statements for scale point 3.


I have a constant sum matrix with three column.

I want to set a validation so that the sum of the 1st column may or may not be 100.

But the column 2 and 3 must be equal to 100.

You can do this with two custom validation conditions, math expressions and piped values. Statement 1-Scale Point 2 should be equal to 100 minus all the other statements for scale point 2 AND Statement 1/Scale Point 3 should be equal to 100 minus all the other statements for scale point 3.

I can see where you’re going with this, but I just wanted to make sure if we’re on the same page.

Since the first column answers are being piped in (Their sum won’t necessarily be equal to 100), I want to make sure that the 2nd and 3rd column’s sum is in fact equal to 100. How can I go about applying validation for this?


Leave a Reply