- Posts: 3
- Thank you received: 0
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 qID = {QID}; var thisQuestion = $('#question'+qID); // A function to shuffle elements or contents of arrays (function($){ $.fn.shuffle = function() { return this.each(function(){ var items = $(this).children(); return (items.length) ? $(this).html($.shuffle(items)) : this; }); } $.shuffle = function(arr) { for( var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x ); return arr; } })(jQuery); // Build an array of codes of all visible sub-questions var sqCodes = []; $('li.answer-item:not(.ls-irrelevant)', thisQuestion).each(function(i) { var thisCode = $(this).attr('id').split('X'+qID)[1]; sqCodes.push(thisCode); }); // Shuffle the sub-question codes array sqCodes = $.shuffle(sqCodes); // Check the first two sub-question codes $('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).prop('checked', true); $('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y'); $('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).prop('checked', true); $('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y'); }); </script>