- Posts: 11
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
// A function to randomly display one row of an array type question function showRandomRow(qid) { // Identify this question var thisQuestion = $('#question'+qid); // Hide the "N/A" column $('table.subquestions-list tr > :last-child', thisQuestion).addClass('na-column').hide(); // If question not previously seen... if($('.na-column input.radio:checked', thisQuestion).length == 0) { // Hide all answer rows $('tr.answers-list', thisQuestion).hide(); // Create an array of answer row IDs var rowIDs = []; $('tr.answers-list', thisQuestion).each(function(i) { rowIDs.push($(this).attr('id')); }); // Shuffle the array rowIDs = $.shuffle(rowIDs); // Show the first row in the array $('#'+rowIDs[0]).removeClass('array1').addClass('array2').show(); // Click "N/A" in all hidden rows $('tr.answers-list:hidden .na-column input.radio', thisQuestion).trigger('click'); } // If question previously seen... else { // Hide answer rows that have "N/A" clicked $('.na-column input.radio:checked', thisQuestion).closest('tr.answers-list').hide(); } } // 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);
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { showRandomRow({QID}); }); </script>