- Posts: 419
- Thank you received: 34
Ask the community, share ideas, and connect with other LimeSurvey users!
<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 answer separated by commas are possible var exclusiveValues = ['A3']; // 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 funstion 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>