@KCL
mabey this code will help you
Qualtrics.SurveyEngine.addOnload(function() {
this.questionclick = function(event, element) {
var orangeTotal = 0;
var greenTotal = 0;
// Calculate the sum of orange and green columns
jQuery("#"+this.questionId+" .ConstantSumInput").each(function() {
var value = parseInt(jQuery(this).val());
if (jQuery(this).hasClass("Q1_1")) { // Check if it's in orange column
orangeTotal += value;
} else if (jQuery(this).hasClass("Q1_2")) { // Check if it's in green column
greenTotal += value;
}
});
// Retrieve previous totals from embedded data
var previousTotalOrange = parseInt("${e://Field/previous_total_orange}");
var previousTotalGreen = parseInt("${e://Field/previous_total_green}");
// Compare with previous totals
if (orangeTotal !== previousTotalOrange || greenTotal !== previousTotalGreen) {
Qualtrics.SurveyEngine.setEmbeddedData('validation_error', 'Total sum does not match the previous value.');
} else {
Qualtrics.SurveyEngine.setEmbeddedData('validation_error', '');
}
}
});
Replace Q1_1
and Q1_2
with the actual column IDs of your Matrix Table question. Also, make sure to adjust the embedded data field names (previous_total_orange
and previous_total_green
) to match what you've set up in your survey.
This script will compare the sum of each column with the corresponding previous total, and if they don't match, it will set an embedded data field (validation_error
) with an error message. You can then use display logic to show an error message to the participant if needed.
Remember to test the survey thoroughly after adding custom JavaScript to ensure it behaves as expected.
Hi @KCL ,
You can try with the below approach and code:
Create embedded data fields to store the total values for each column. Let's call them TotalColumn1 and TotalColumn2.
When respondents submit the values for each column, calculate and store the total for each column using JavaScript.
///Code////
Qualtrics.SurveyEngine.addOnload(function() {
// Retrieve stored total values
var totalColumn1 = "${e://Field/TotalColumn1}";
var totalColumn2 = "${e://Field/TotalColumn2}";
// Calculate total values entered by the respondent
var enteredTotal1 = 0;
var enteredTotal2 = 0;
// Change "Q1", "Q2", and "Q3" to the respective question IDs for each column
var q1Inputs = document.querySelectorAll('#Q1 inputetype="text"]');
var q2Inputs = document.querySelectorAll('#Q2 inputqtype="text"]');
for (var i = 0; i < q1Inputs.length; i++) {
enteredTotal1 += parseFloat(q1Inputsei].value) || 0;
}
for (var i = 0; i < q2Inputs.length; i++) {
enteredTotal2 += parseFloat(q2Inputsi].value) || 0;
}
// Validate totals
if (enteredTotal1 !== parseFloat(totalColumn1) || enteredTotal2 !== parseFloat(totalColumn2)) {
this.disableNextButton();
alert("The total sum of Column 1 must match " + totalColumn1 + " and the total sum of Column 2 must match " + totalColumn2 + ".");
} else {
this.enableNextButton();
}
});
Thank You!
@Nanditha MM , thanks for sharing this code and linking to other useful Community posts!