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 a single-column multiple choice exclusive function excludeOpt(qID) { var thisQuestion = $('#question' + qID); // Add classes to the checkbox items $('input[type="checkbox"]', thisQuestion).each(function(i) { $(this).closest('li').addClass('normal-item'); }); // Mark the last checkbox as exclusive $('li.normal-item:last', thisQuestion).removeClass('normal-item').addClass('exclusive-item'); // A listener on the checkboxes $('input[type="checkbox"]', thisQuestion).on('change', function(event) { handleExclusive($(this).closest('li')); }); function handleExclusive(thisItem) { // Uncheck the appropriate boxes if ($(thisItem).hasClass('normal-item')) { $('.exclusive-item input[type="checkbox"]', thisQuestion).prop('checked', false); } else { $('.normal-item input[type="checkbox"]', thisQuestion).prop('checked', false); } // Check conditions (relevance) $('li.checkbox-item', thisQuestion).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'); }); } } </script>
<script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function() { // Identify this question var thisQuestion = $('#question{QID}'); // Identify the "exclusive" column(s) // Multiple columns separated by commas var exclusiveCols = [1,2]; // Assign classes to various elements $('tr[id^="javatbd"]', thisQuestion).each(function(i){ var column = 1; var scale = 1; $('td', this).each(function(i) { if($(this).hasClass('answer-item')) { $(this).addClass('scale-'+scale+' column-'+column+''); column++; } else if(scale == 1 && $(this).hasClass('dual_scale_separator')) { column = 1; scale = 2; } else if(scale == 2 && $(this).hasClass('dual_scale_separator')) { column = 1; scale = 3; } }); $('td.scale-2:last', this).addClass('na-item'); }); $(exclusiveCols).each(function(i) { $('td.scale-1.column-'+this, thisQuestion).addClass('exclusive-item'); }); // Hide the "N/A" column var naIndex = $('td.na-item:eq(0)', thisQuestion).index(); $('td.na-item', thisQuestion).hide(); $('.questions-list .dsheader:last', thisQuestion).attr('colspan', Number($('.questions-list thead tr.groups .dsheader:last', thisQuestion).attr('colspan'))-1); $('.questions-list thead tr:not(.groups) th.answer-text:not(.noanswer-text):last', thisQuestion).hide(); $('colgroup.group-2 col:last', thisQuestion).hide(); // Listener on the radios $('td.scale-1 input:radio', thisQuestion).on('click', function(e) { var thisCell = $(this).closest('td'); var thisRow = thisCell.closest('tr'); var thisNA = $('.na-item input:radio', thisRow); var naValue = ''; if(thisCell.hasClass('exclusive-item')) { naValue = thisNA.val(); thisNA.trigger('click'); $('td.scale-2:not(.na-item) input:radio', thisRow).prop('disabled', true); } else { thisNA.prop('checked', false); $('td.scale-2:not(.na-item) input:radio', thisRow).prop('disabled', false); } // Fire ExpressionScript naName = thisNA.attr("name"); naName = naName.replace('#', '_'); $("#java" + naName).val(naValue); ExprMgr_process_relevance_and_tailoring('change', naValue, 'radio'); }); // Initial states $('td.exclusive-item input:radio:checked', thisQuestion).each(function(i) { var thisRow = $(this).closest('tr'); console.log(thisRow); $('.na-item input:radio', thisRow).trigger('click'); $('td.scale-2:not(.na-item) input:radio', thisRow).prop('disabled', true); }); }); </script>
<script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ var thisQuestion = $('#question{QID}') // Add some classes to the checkbox cells $('tr[id^="javatbd"]', thisQuestion).each(function(i) { // Last two columns exclusive $('td.checkbox-item', this).slice(-2).addClass('exclusive-item'); }); // A function to un-check boxes function resetCheckbox(thisItem) { $(':hidden', thisItem).val(''); $(':checkbox', thisItem).prop('checked', false).trigger('change'); } // A listener on the checkboxes $(':checkbox', thisQuestion).on('change', function(e) { if($(this).is(':checked')) { var thisItem = $(this).closest('.answer-item'); var thisRow = $(this).closest('tr'); var items = $('td.answer-item.exclusive-item', thisRow); if($(thisItem).hasClass('exclusive-item')) { items = $('td.answer-item', thisRow).not(thisItem); } $.each(items, function(i, el) { resetCheckbox(el); }); } }); }); </script>