- Posts: 157
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
Please Log in to join the conversation.
Please Log in to join the conversation.
<script type="text/javascript" charset="utf-8"> function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } $(document).on('ready pjax:scriptcomplete',function(){ // Define the code that has to be fixed var fixedCode= 7; // Fill the array with all codes, here Numbers fom 1 to 10 var arr = []; for (var i = 1; i < 11; i++) { arr.push(i); } arr = shuffle(arr); // Find the fixed code const index = arr.indexOf(fixedCode); if (index > -1) { // only splice array when item is found arr.splice(index, 1); // remove the element } // Insert the fixed code at his position arr.splice(fixedCode-1, 0, fixedCode); $('#question{QID} input[type="text"]').val(arr); }); </script>
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ //Identify this question var thisQuestion = $('#question{QID}'); var thisAnswerList = $('li.answer-item:eq(0)', thisQuestion).parent(); // Retrieve the answer codes from the "QOrder" question var answerCodes = '{QOrder}'.split(','); // Loop through the answer codes $.each(answerCodes, function(i, val) { // Move the answer item $(thisAnswerList).append($('li.answer-item[id$="X{QID}'+val+'"]', thisQuestion)); }); }); </script>
Please Log in to join the conversation.
Please Log in to join the conversation.
You can use whatever you wantfixedCode may be 1 digit or more than 1 digit
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
<script type="text/javascript" charset="utf-8"> function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } $(document).on('ready pjax:scriptcomplete',function(){ function removeCode(value) { var index = arr.indexOf(value); if (index > -1) { // only splice array when item is found arr.splice(index, 1); // 2nd parameter means remove one item only } } function addCode(value) { arr.splice(value-1, 0, value); } var fixedArray= [7,13,3,8]; // Order of codes doesn't matter; in the next line it is sorted fixedArray.sort(function(a, b) { return a - b } ); // Fill the array; here codes from 1 to 30 var arr = []; for (var i = 1; i < 31; i++) { arr.push(i); } // First remove the codes that are to be fixed fixedArray.forEach(removeCode); // Shuffle the array arr = shuffle(arr); // and insert the fixed codes at their place fixedArray.forEach(addCode); $('#question{QID} input[type="text"]').val(arr); }); </script>
Please Log in to join the conversation.