- Posts: 40
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var thisQuestion = $('#question{QID}'); // Identify the "exclusive" column(s) // Multiple columns separated by commas var exclusiveCols = [3]; // Assign classes to various elements $('.answers-list', thisQuestion).each(function(i){ var column = 1; var scale = 1; $('td', this).each(function(i){ if($(this).hasClass('radio-item')) { $(this).addClass('scale-'+scale+' column-'+column+''); column++; } else { column = 1; scale = 2; } }); $('td:last', this).addClass('na-item'); }); $(exclusiveCols).each(function(i) { $('td.scale-1.column-'+this, thisQuestion).addClass('exclusive-item'); }); // Hide the "N/A" column $('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:last', thisQuestion).hide(); var colsWidth = $('col.odd:eq(0)', thisQuestion).attr('width').replace(/%/, '')*$('col.odd, col.even', thisQuestion).length; var newColWidth = colsWidth/($('col.odd, col.even', thisQuestion).length-1); $('col.odd, col.even', thisQuestion).attr('width', newColWidth+'%'); // Listener on the radios $('td.scale-1 input.radio', thisQuestion).click(function(e) { var thisCell = $(this).closest('td'); var thisRow = thisCell.closest('tr'); if(thisCell.hasClass('exclusive-item')) { $('.na-item input.radio', thisRow).trigger('click'); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', true); } else { $('.na-item input.radio', thisRow).prop('checked', false); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', false); } }); // Initial states $('td.exclusive-item input.radio:checked', thisQuestion).each(function(i) { var thisRow = $(this).closest('tr'); $('.na-item input.radio', thisRow).trigger('click'); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', true); }); }); </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var thisQuestion = $('#question{QID}'); // Identify the "exclusive" column(s) // Multiple columns separated by commas var exclusiveCols = [2,3]; // Assign classes to various elements $('.answers-list', thisQuestion).each(function(i){ var column = 1; var scale = 1; $('td', this).each(function(i){ if($(this).hasClass('radio-item')) { $(this).addClass('scale-'+scale+' column-'+column+''); column++; } else if($(this).hasClass('dual_scale_separator')) { column = 1; scale = 2; } }); $('td:last', this).addClass('na-item'); }); $(exclusiveCols).each(function(i) { $('td.scale-1.column-'+this, thisQuestion).addClass('exclusive-item'); }); // Hide the "N/A" column $('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:last', thisQuestion).hide(); var responsesWidth = $.trim($.trim($('.col-responses col:eq(0)', thisQuestion).attr('style').split('width:')[1]).replace(/%;/, '')); var colsWidth = Number(responsesWidth)*$('.col-responses col', thisQuestion).length; var newResponsesWidth = colsWidth/($('.col-responses col', thisQuestion).length-1); $('.col-responses col', thisQuestion).css('width', newResponsesWidth+'%'); // Listener on the radios $('td.scale-1 input.radio', thisQuestion).click(function(e) { var thisCell = $(this).closest('td'); var thisRow = thisCell.closest('tr'); if(thisCell.hasClass('exclusive-item')) { $('.na-item input.radio', thisRow).trigger('click'); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', true); } else { $('.na-item input.radio', thisRow).prop('checked', false); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', false); } }); // Initial states $('td.exclusive-item input.radio:checked', thisQuestion).each(function(i) { var thisRow = $(this).closest('tr'); $('.na-item input.radio', thisRow).trigger('click'); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', true); }); }); </script>
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify this question var thisQuestion = $('#question{QID}'); // Identify the "exclusive" answer(s) in scale one // Multiple answers separated by commas are possible var exclusiveValues = ['A4','A5']; // Hide the "N/A" option in scale 2 // Set this to false if you want that option always visible var hideNA = true; // Identify the "N/A" value in scale 2 var naValue = $('select[id$="_1"]:eq(0) option:last', thisQuestion).attr('value'); // A function to handle the drop-down behaviour function handleSelects(thisSelect) { var selectVal = $(thisSelect).val(); var thisRow = $(thisSelect).closest('tr'); var exclusive = false; $.each(exclusiveValues, function(i, val) { if(selectVal == val) { exclusive = true; } }); if(exclusive == true) { if(hideNA == true) { $('select[id$="_1"] option[value="'+naValue+'"]', thisRow).toggleOption(true); } $('select[id$="_1"]', thisRow).val(naValue).prop('disabled', true); } else { $('select[id$="_1"]', thisRow).prop('disabled', false); if(hideNA == true) { $('select[id$="_1"] option[value="'+naValue+'"]', thisRow).toggleOption(false); } } } // Listener on the drop-downs $('select[id$="_0"]', thisQuestion).on('change', function(e) { handleSelects($(this)); }); // Initial states $('select[id$="_0"]', thisQuestion).each(function(i) { handleSelects($(this)); }); // On submit $('#ls-button-submit').on('click', function(e) { $('select[id$="_1"]', thisQuestion).prop('disabled', false); }); }); $.fn.toggleOption = function(show) { jQuery(this).toggle(show); if(show) { if( jQuery(this).parent('span.toggleOption').length) jQuery(this).unwrap(); } else { if(jQuery(this).parent('span.toggleOption' ).length == 0) jQuery(this).wrap( '<span class="toggleOption" style="display: none;" />'); } }; </script>