Require only one check for grid question

From Catglobe Wiki
Jump to: navigation, search

Challenge

Normally, the grid question require one answer for each sub question to navigate to next question. Now, we want it require only one check for the whole grid.
Example
OneCheckRequired.png

Solution

  • Create a grid question
  • Add the below script to that question

Code

 1 var normalQuestionCheck = questioncheck;
 2 
 3 function extendedQuestionCheck()
 4 {
 5    ErrorMessages.getInstance().clearErrorMessages();//clear old error message
 6    var valid = false;
 7    var q_name = "QUESTION." + "GridQuestionLabel";//question's name = "QUESTION." + question label
 8    var n = $(".grid_inner")[0].rows.length - 1;//number of sub questions
 9 
10    //check if each answer option for each sub question is checked
11    for(var i=0;i<n;i++)
12 	{
13 		$("input:checkbox[name='"+q_name+"."+i+"']").each(
14 			function()
15 			{
16 				if($(this).attr("checked"))
17 					valid = true;				
18 			}
19 		);
20 		if(valid)
21 		break;
22 	}
23 	
24    if(!valid) showError(quest.requiredtext);
25    return valid;
26          
27 }
28 
29 function showError(e)
30 {
31    ErrorMessages.getInstance().showErrorMessage(e);
32 }
33 
34 questioncheck = extendedQuestionCheck;

Source

Questionnaire Resource Id on cg.catglobe.com site: 164079 (Question: Q9_Require_only_one_check_for_grid_question)