- Posts: 10223
- Thank you received: 3640
Ask the community, share ideas, and connect with other LimeSurvey users!
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Call the exclude function using question ID excludeOpt ({QID}); }); // A function to make the last option in each array row exclusive function excludeOpt (qID) { var thisQuestion = $('#question'+qID) // Add some classes to the checkbox cells $('table.question tbody td', thisQuestion).addClass('normalOpt'); $('table.question tbody tr', thisQuestion).each(function(i) { // Last two coluns are exclusive $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt'); $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt'); }); // A listener on the checkboxes $('table.question tbody td input[type=checkbox]', thisQuestion).click(function (event) { handleCheckboxes($(this).closest('td')) }); // A listener on the checkbox cells $('table.question td.question-item', thisQuestion).click(function (event) { handleCheckboxes($(this)); }); function handleCheckboxes(thisCell) { // Set some vars var thisRow = thisCell.closest('tr'); var thisInput = $('input[type=checkbox]', thisCell); if(thisInput.is(':checked')) { // Uncheck the appropriate boxes in the row if ($(thisCell).hasClass('normalOpt')) { // Non-exclusive $('.exlusiveOpt input[type=checkbox]', thisRow).each(function(i) { $(this).attr('checked', false); $(this).closest('td').find('input[type="hidden"]').attr('value', ''); }); } else { // Exclusive $('input[type=checkbox]', thisRow).not(thisInput).each(function(i) { $(this).attr('checked', false); $(this).closest('td').find('input[type="hidden"]').attr('value', ''); }); } } } } </script>
tpartner wrote: Try this to make the last two columns exclusive:
Code:<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Call the exclude function using question ID excludeOpt ({QID}); }); // A function to make the last option in each array row exclusive function excludeOpt (qID) { var thisQuestion = $('#question'+qID) // Add some classes to the checkbox cells $('table.question tbody td', thisQuestion).addClass('normalOpt'); $('table.question tbody tr', thisQuestion).each(function(i) { // Last two coluns are exclusive $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt'); $('.normalOpt:last', this).removeClass('normalOpt').addClass('exlusiveOpt'); }); // A listener on the checkboxes $('table.question tbody td input[type=checkbox]', thisQuestion).change(function (event) { if($(this).is(':checked')) { // Set some vars var thisRow = $(this).closest('tr'); var thisCell = $(this).closest('td'); // Uncheck the appropriate boxes in the row if ($(thisCell).hasClass('normalOpt')) { // Non-exclusive $('.exlusiveOpt input[type=checkbox]', thisRow).each(function(i) { $(this).prop('checked', false); $(this).closest('td').find('input[type="hidden"]').attr('value', ''); }); } else { // Exclusive $('input[type=checkbox]', thisRow).not(this).each(function(i) { $(this).prop('checked', false); $(this).closest('td').find('input[type="hidden"]').attr('value', ''); }); } } }); } </script>
// A jQuery plugin to render column(s) in checkbox arrays exclusive (function( $ ){ $.fn.cbExclusiveColumns = function(options) { var opts = $.extend( { //columns: [3,4] // Column(s) to be exclusive (comma-separated) }, options); return this.each(function() { var thisQuestion = $(this) // Add some classes to the checkbox cells $('td.answer-item', thisQuestion).addClass('normal-item'); $('tr.subquestion-list', thisQuestion).each(function(i) { var thisRow = $(this); $.each(opts.columns, function(i, val) { $('td.answer-item:eq('+(val-1)+')', thisRow).removeClass('normal-item').addClass('exclusive-item'); }); }); // A listener on the checkboxes $('input[type="checkbox"]', thisQuestion).on('change', function (event) { handleExclusive($(this).closest('td')); }); function handleExclusive(thisCell) { var thisRow = $(thisCell).closest('tr'); // Uncheck the appropriate boxes in a row if ($(thisCell).hasClass('normal-item')) { $('.exclusive-item input[type="checkbox"]', thisRow).prop('checked', false); $('.exclusive-item input[type="text"]', thisRow).val(''); } else { $('.answer-item', thisRow).not(thisCell).find('input[type="checkbox"]').prop('checked', false); $('.answer-item', thisRow).not(thisCell).find('input[type="text"]').val(''); } // Check conditions (relevance) $('td.answer-item', thisRow).each(function(i) { var thisValue = ''; if($('input[type="checkbox"]', this).is(':checked')) { thisValue = 1; } var thisSGQA = $('input[type="checkbox"]', this).attr('id').replace(/cbox_/, ''); $('input[type="hidden"]', this).attr('value', thisValue); fixnum_checkconditions(thisValue, thisSGQA, 'hidden'); }); } }); }; })( jQuery );
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ $('#question{QID}').cbExclusiveColumns({ columns: [3,4] // Column(s) to be exclusive (comma-separated) }); }); </script>
davebostockgmail wrote: Following on from this is there any way to make the last row of an array numbers with checkboxes exclusive
For example
A B
Option 1 [ ] [ ]
Option 2 [ ] [ ]
Exclusive [ ] [ ]
So in this case you can select both option 1 and 2 as a multiple but the exclusive option behaves as it would in a multiple choice question?
Version 3.17.5
Thanks
Dave