Welcome to the LimeSurvey Community Forum

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

Randomise order of a list

  • Matadeleo
  • Matadeleo's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 8 months ago - 3 years 8 months ago #203690 by Matadeleo
Randomise order of a list was created by Matadeleo
Hi guys,

I have a survey where I have 11 products, I need to select 4 to test and then ask questions regarding each of the 4 products selected. If I ask this as a multi-choice, is there any way to pull those back out but in a random order, to be used as text subs in the following parts of the survey?

Unfortunately group randomisation is not possible, as each product runs over many groups, and then needs to be repeated.

Ideally I'd like to have 4 equation questions following the list of products, each with a product as the value, but assigned randomly.

Is this possible? Happy to hear alternative suggestions
Last edit: 3 years 8 months ago by Matadeleo.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #203704 by tpartner
Replied by tpartner on topic Randomise order of a list
1) Place a hidden (via CSS class) multiple-short-text question with 4 sub-questions directly after the multiple-choice.

2) Add this script to the source of the multiple-short-text. It will create a randomized array of checked labels and load the inputs in the multiple-short-text with those values. These can then be used later in the survey.

Code:
<script type="text/javascript" charset="utf-8">  
 
  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;
  }  
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify some stuff
    var thisQuestion = $('#question{QID}');
    var prevQuestion = thisQuestion.prev('.question-container');
 
    // Listener on the checkboxes
    $(':checkbox', prevQuestion).on('change', function(i) {
      // Create a randomized array of the checked labels
      var strings = [];
      $(':checkbox:checked', prevQuestion).each(function(i) {
        strings.push($(this).next('label').text());
      });
      shuffleArray(strings);
 
      // Load the hidden question
      $('input:text', thisQuestion).val('').trigger('keyup');
      $.each(strings, function(i, value) {
        $('input:text:eq('+i+')', thisQuestion).val(value).trigger('keyup');
      });  
    });
    });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...9116.lss
File Size:28 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: Matadeleo
The topic has been locked.
  • Matadeleo
  • Matadeleo's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 8 months ago #203717 by Matadeleo
Replied by Matadeleo on topic Randomise order of a list
Thanks, exactly what I was looking for!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose