Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Call the function
countChecked('{SGQ}');
function countChecked(sgq) {
// Identify the questions
var sID = sgq.split('X')[0];
var gID = sgq.split('X')[1];
var qHiddenID = sgq.split('X')[2];
var qHidden = $('#question'+qHiddenID+'');
if($(qHidden).length > 0) {
var q1 = $(qHidden).prevAll('.array-flexible-row:eq(0)');
var q1ID = $(q1).attr('id').split('question')[1];
var q2 = $(qHidden).nextAll('.array-flexible-row:eq(0)');
var q2ID = $(q2).attr('id').split('question')[1];
}
// Hide the hidden question
$('#question'+qHiddenID+'').hide();
// Assign column-specific classes to Q1
$('.answers-list', q1).each(function(i){
$('td.answer-item', this).each(function(i){
$(this).addClass('answer-column-'+(i+1)+'');
});
});
// A listener on the 1st column of Q1 radio buttons
$('.answer-column-1, .answer-column-2', q1).click(function () {
handleHiddenInput (this, 'checked');
});
// A listener on the 2nd and 3rd columns of Q1 radio buttons
$('.answer-column-3, .answer-column-4, .answer-column-5, .answer-column-6, .answer-column-7', q1).click(function () {
handleHiddenInput (this, 'unchecked');
});
// A function to toggle the hidden checkboxes
function handleHiddenInput(trigger, state) {
// Identify some stuff
var rowID = $(trigger).closest('tr.answers-list').attr('id');
var tmp = rowID.split('X'+q1ID);
var answerCode = tmp[1];
var hiddenInput = $('#answer'+sID+'X'+gID+'X'+qHiddenID+answerCode);
// Toggle the input depending on "state"
var checkedState = false;
if(state == 'checked') {
checkedState = true;
}
$(hiddenInput).prop('checked', checkedState);
// Fire the conditions function to hide the corresponding row in Q2
var hiddenInputValue = $(hiddenInput).attr('value');
var hiddenInputName = $(hiddenInput).attr('name');
var hiddenInputType = $(hiddenInput).attr('type');
checkconditions(hiddenInputValue, hiddenInputName, hiddenInputType);
// Fix up the q2 row background colours
$('.answers-list:visible', q2).each(function(i, el){
$(el).removeClass('array1, array2');
if(i % 2 == 0) {
$(el).addClass('array2');
}
else {
$(el).addClass('array1');
}
});
}
}
});
</script>