Code:
<script type="text/javascript" data-author="Tony Partner">
$(document).ready(function() {
var thisQuestion = $('#question{QID}');
// Add column and row specific classes
$('tr.subquestion-list', thisQuestion).each(function(i) {
var rowIndex = i;
$('th, td', this).each(function(i) {
$(this).addClass('column-'+i+' row-'+rowIndex).attr('data-column', i).attr('data-row', rowIndex);
});
});
// Add some classes to the checkbox cells
$('.answer-item:nth-child(2), .answer-item:nth-child(3)', thisQuestion).addClass('exclusive-item row-exclusive set-1').attr('data-set', 1);
$('.answer-item:nth-child(4)', thisQuestion).addClass('exclusive-item row-exclusive set-2').attr('data-set', 2);
$('.answer-item:nth-child(5)', thisQuestion).addClass('exclusive-item row-exclusive set-3').attr('data-set', 3);
// A listener on the checkboxes
$('.answer-item input[type="checkbox"]', thisQuestion).change(function (event) {
handleExclusive($(this).closest('.answer-item'));
});
function handleExclusive(thisCell) {
var thisCheckBox = $(':checkbox', thisCell);
if(thisCheckBox.is(':checked')) {
var thisRow = $(thisCell).attr('data-row');
var thisColumn = $(thisCell).attr('data-column');
var thisSet = $(thisCell).attr('data-set');
var excludeCells = $('.answer-item[data-row="'+thisRow+'"]').not('[data-set="'+thisSet+'"]');
// Uncheck those items
$(excludeCells).each(function(i) {
$('input:hidden', this).val('');
$(':checkbox', this).prop('checked', false).trigger('change');
});
}
}
});
</script>