- Posts: 5
- Thank you received: 1
Ask the community, share ideas, and connect with other LimeSurvey users!
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Function to allow randomization of all answers except the last one in multiple numeric input questions function partRand(sID, gID, qID) { // Find the number of answers var ansCount = '' $( 'div#question' + qID + ' div.answers li' ).each(function(i) { ansCount = (i - 1); }); // Place the last answer created at the end of the list - but before the first helping sum $( 'li#javatbd' + sID + 'X' + gID + 'X' + qID + ansCount + '' ).insertBefore($( 'div#question' + qID + ' div.answers li.multiplenumerichelp' ).first()); } // Call the function with the SID, GID and QID partRand(SID, GID, QID); });
Please add your solution!Thomas wrote: For future reference I would integrate my solution into the wiki.
Mazi wrote: Have a look at these workarounds:
docs.limesurvey.org/Workarounds%3A+Manip...List_radio_questions
docs.limesurvey.org/Workarounds%3A+Manip...t_dropdown_questions
<script type="text/javascript"> $(document).ready(function() { // Function to allow randomization of all answers except the last three in Multiple options and List/radio questions function partRand(sID, gID, qID) { // Find the number of answers var ansCount = '' lastthreeitems=new Array(); var liid=new Array(); var $ul; var j=0; $( '#question' + qID + ' td.answer li' ).each(function(i) { ansCount = (i + 1); }); $( '#question' + qID + ' td.answer li' ).each(function(i) { if(i>ansCount-4){ lastthreeitems[j]=$(this).html(); liid[j]=$(this).attr("id"); j++; $(this).remove(); } }); $( '#question' + qID + ' td.answer li' ).each(function(i) { // get current ul $ul = $(this).parent(); // get array of list items in current ul var $liArr = $ul.children('li'); // sort array of list items in current ul randomly $liArr.sort(function(a,b){ // Get a random number between 0 and 100 var temp = parseInt( Math.random()*100 ); // Get 1 or 0, whether temp is odd or even var isOddOrEven = temp%2; // Get +1 or -1, whether temp greater or smaller than 5 var isPosOrNeg = temp>5 ? 1 : -1; // Return -1, 0, or +1 return( isOddOrEven*isPosOrNeg ); }) // append list items to ul .appendTo($ul); }); $.each(lastthreeitems,function(i){ $( '#question' + qID + ' td.answer ul' ).append("<li id="+liid[i]+">"+this+"</li>"); }); } // Call the function with the SID, GID and QID partRand(SSSSS, GG, QQ); }); </script>
<script type="text/javascript"> $(document).ready(function() { // Function to allow randomization of all answers except the last one in Multiple options and List/radio questions function partRand(sID, gID, qID) { //var to define how many lists to be added at the end var insertitems=3; // Find the number of answers var ansCount = '' lastitems=new Array(); var liid=new Array(); var $ul; var j=0; $( '#question' + qID + ' td.answer li' ).each(function(i) { ansCount = (i + 1); }); $( '#question' + qID + ' td.answer li' ).each(function(i) { if(i>ansCount-(insertitems+1)){ lastitems[j]=$(this).html(); liid[j]=$(this).attr("id"); j++; $(this).remove(); } }); $( '#question' + qID + ' td.answer li' ).each(function(i) { // get current ul $ul = $(this).parent(); // get array of list items in current ul var $liArr = $ul.children('li'); // sort array of list items in current ul randomly $liArr.sort(function(a,b){ // Get a random number between 0 and 100 var temp = parseInt( Math.random()*100 ); // Get 1 or 0, whether temp is odd or even var isOddOrEven = temp%2; // Get +1 or -1, whether temp greater or smaller than 5 var isPosOrNeg = temp>5 ? 1 : -1; // Return -1, 0, or +1 return( isOddOrEven*isPosOrNeg ); }) // append list items to ul .appendTo($ul); }); $.each(lastitems,function(i){ $( $ul ).append("<li id="+liid[i]+">"+this+"</li>"); }); } // Call the function with the SID, GID and QID partRand(SSSSS, GG, QQ); }); </script>
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify this question ID var qID = '{QID}'; // List the sub-question codes to be placed at the end of the list // (comma-separated) var lastItems = ['SQ007','SQ008']; // Insert the last items $.each(lastItems, function(i, val) { $('#question{QID} tr[id^="javatbd"]:last').after($('#question{QID} tr[id^="javatbd"][id$="X'+qID+val+'"]')); }); }); </script>