- Posts: 5
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
fransmarcelissen wrote: Often there are smart solutions and simple solution. The simple solution here is to do the randomization externally and read it in into a token attribute. Then you do'nt need the hidden question anymore.
In addition: you use an equation question with a rand(). The problem with this is that rand() is evaluated again and again, each time with a different result. It is simpler to use a hidden text field and set the default value to rand(). Now the expression is executed only once en keeps its value.
Succes
Frans
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // The array of numbers to be randomized var conditionNumbers = [1, 2, 3, 4, 5, 6, 7, 8]; // Identify this question var thisQuestion = $('#question{QID}'); // Hide this question thisQuestion.hide(); // Shuffle the array conditionNumbers = $.shuffle(conditionNumbers); // Load the first array value into this question if($.trim($('input.text', thisQuestion).val()) == '') { $('input.text', thisQuestion).val(conditionNumbers[0]); } }); // 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>