- Posts: 64
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var qID = {QID}; var thisQuestion = $('#question'+qID); // First group of sub-question codes var sqCodes1 = ['SQ001', 'SQ002', 'SQ003']; // Second group of sub-question codes var sqCodes2 = ['SQ004', 'SQ005', 'SQ006']; // Shuffle the subquestions shuffleArray(sqCodes1); shuffleArray(sqCodes2); // Insert the rows in the shuffled order $(sqCodes1).each(function(i, val){ $('.subquestions-list', thisQuestion).append($('.question-item[id$="X'+qID+val+'"]', thisQuestion)); }); $(sqCodes2).each(function(i, val){ $('.subquestions-list', thisQuestion).append($('.question-item[id$="X'+qID+val+'"]', thisQuestion)); }); }); </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var qID = {QID}; var thisQuestion = $('#question'+qID); // First group of sub-question codes var sqCodes1 = ['SQ001', 'SQ002', 'SQ003']; // Second group of sub-question codes var sqCodes2 = ['SQ004', 'SQ005', 'SQ006']; // Shuffle the subquestions shuffleArray(sqCodes1); shuffleArray(sqCodes2); // Insert the rows in the shuffled order $(sqCodes1).each(function(i, val){ $('table.subquestions-list', thisQuestion).append($('.answers-list[id$="X'+qID+val+'"]', thisQuestion)); }); $(sqCodes2).each(function(i, val){ $('table.subquestions-list', thisQuestion).append($('.answers-list[id$="X'+qID+val+'"]', thisQuestion)); }); }); </script>
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var qID = {QID}; var thisQuestion = $('#question'+qID); // First group of answer codes var answerCodes1 = ['A1', 'A2', 'A3']; // Second group of answer codes var answerCodes2 = ['A4', 'A5', 'A6']; // Shuffle the subquestions shuffleArray(answerCodes1); shuffleArray(answerCodes2); // Insert the rows in the shuffled order $(answerCodes1).each(function(i, val){ $('.answers-list', thisQuestion).append($('.answer-item[id$="X'+qID+val+'"]', thisQuestion)); }); $(answerCodes2).each(function(i, val){ $('.answers-list', thisQuestion).append($('.answer-item[id$="X'+qID+val+'"]', thisQuestion)); }); }); </script>
<script type="text/javascript" charset="utf-8"> function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } $(document).on('ready pjax:scriptcomplete',function(){ // Identify this question var qID = {QID}; var thisQuestion = $('#question'+qID); var sqGroupIndexes = [1, 2, 3]; // The groups of sub-codes var sqCodes = { 1: ['1', '2', '3'], 2: ['4','5', '6'], 3: ['7','8'] } // Shuffle the arrays shuffleArray(sqGroupIndexes)[1]; shuffleArray(sqCodes[1]); shuffleArray(sqCodes[2]); shuffleArray(sqCodes[3]); // Loop though the randomized indexes $.each(sqGroupIndexes, function(index, val) { // Insert the rows in the shuffled order $.each(sqCodes[val], function(index, val){ $('.answers-list[id$=X'+qID+val+']', thisQuestion).appendTo($('table.subquestion-list', thisQuestion)); }); }); }); </script>