Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Same randomization order for following questions

More
3 years 7 months ago #204496 by oksana
Hi guys, I was looking for this solution Tony is written about for some long period of time. I imported the last survey example in my LimeSurvey 3.6.3. Does anybody know what should I change to make it work in my version?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago - 3 years 7 months ago #204513 by tpartner
This will maintain the order in multiple-choice questions, in different groups, using columns, in version 3.22.210.

(if using an earlier 3.x version, disable AJAX in the survey theme options)

In the first question:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
 
    //Identify the questions
    var thisQuestion = $('#question{QID}');
    var hiddenQuestion = $(thisQuestion).nextAll('.text-short:eq(0)');
 
    // Create an array of answer codes
    var answerCodes = [];
    $('.answer-item', thisQuestion).each(function(i) {
      answerCodes.push($(this).attr('id').split('X{QID}')[1]);
    });
 
    // Load the hidden question
    $('input:text', hiddenQuestion).val(answerCodes);
 
  });
</script>

In following questions:

Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
 
    //Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Determine the number of items in the first column
    var rowCount = $('.answers-list.multiple-list > ul:eq(0) .question-item', thisQuestion).length;
 
    // Retrieve the answer codes from the "randomOrder" question
    var answerCodes = '{randomOrder}'.split(',');
    console.log(answerCodes);
 
    // Loop through the answer codes
    var row = 0;
    var column = 0;
    $.each(answerCodes, function(i, val) {
 
      // Move the answer item
      $('.answers-list.multiple-list > ul:eq('+column+')', thisQuestion).append($('.answer-item[id$="X{QID}'+val+'"]', thisQuestion));
 
      if(row < (rowCount-1)) {
        row++;
      }
      else {
        // Increment the column
        row = 0;
        column++;
      }
    });
 
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...9(1).lss
File Size:74 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 7 months ago by tpartner.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose