- Posts: 169
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
var questionArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; shuffleArray(questionArr); $(document).ready(function() { for(var i = 0;i<10;i++) { $('.text:eq('+i+')').val(questionArr[i]); } }); 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() { // Hide this question $('#question{QID}').hide(); // An array defining total number of questions in the set and number to be shown // In this case, 4 of 10 will be shown var setArray = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0]; // Shuffle the array setArray = $.shuffle(setArray); // Loop Through the array and populate the sub-questions $(setArray).each(function(i, value) { if($('#question{QID} input.text:eq('+i+')').val() == '') { $('#question{QID} input.text:eq('+i+')').val(value); } }); }); // A function to shuffle 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>