Toggle menu
862
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

This solution will help your answer will total 100%.

First create a textgrid question, then add the below script to Javascript tab

//
// Private helper functions
//
function sumUp(question) {
   // Sum up all except the last sub question, which is used for the s
   var sum = 0;
   question.subQuestions.slice(0, question.subQuestions.length - 1).each(function(sq) {
      sum += parseInt(sq.attr('answer')) || 0;
   });
   return sum;
}

//
// Add an extra row to contain the sum
//
Question.bind('afterShowQuestion', function(ev, question, jqe) {
   var sum = sumUp(question);
   var sumElt = jqe.find('tr:last input').prop('disabled', true).val(sum);
});

//
// Listen for changes to the answer and update the sum
//
Question.bind('answerChanged', function(ev, question, subQuestion) {
   // Ignore answer changes for the last sub question
   if (subQuestion.gridNumber == question.subQuestions.length - 1) {
      return;
   }
   question.subQuestions[question.subQuestions.length - 1].attr('answer', sumUp(question));
});

//
// Validate that the sum is exactly 100
//
Question.bind('afterValidateQuestion', function(ev, question, state) {
   var sum = sumUp(question);
   if (sum != 100) {
      state.valid = false;
      question.attr('errorMessage', 'Du bedes angive dine svar fra 0-100%, så dit svar tilsammen giver 100%');
   }
});