Welcome to the LimeSurvey Community Forum

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

random auto select options in multiple choice question

  • agustinface
  • agustinface's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 10 months ago #168988 by agustinface
Hi All
This is what I need:
I have a first multiple choice question and in the next I want to randomly select (javascript?) two of the options you have chosen in the previous question.
I am using version 3.7.2 of limesurvey.
I attach an example survey.
Can somebody help me? Thank you very much!
I used some examples that I found in the forum but none worked.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 10 months ago #168991 by tpartner
Can the question be on separate pages so the filtering doesn't mess with JavaScript?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • agustinface
  • agustinface's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 10 months ago #168994 by agustinface
Yes, the question can be on separate pages.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 10 months ago #168996 by tpartner
Okay, I am travelling today but will provide a JavaScript snippet tomorrow.

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: agustinface
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 10 months ago #169021 by tpartner
This script will randomly select two visible check-boxes.

Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
    // Identify this question
    var qID = {QID};
    var thisQuestion = $('#question'+qID);
 
    // 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);
 
    // Build an array of codes of all visible sub-questions
    var sqCodes = [];
    $('li.answer-item:not(.ls-irrelevant)', thisQuestion).each(function(i) {
      var thisCode = $(this).attr('id').split('X'+qID)[1];
      sqCodes.push(thisCode);
    });
 
    // Shuffle the sub-question codes array
    sqCodes = $.shuffle(sqCodes);
 
    // Check the first two sub-question codes
    $('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).prop('checked', true);
    $('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');
    $('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).prop('checked', true);
    $('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');
  });  
</script>

Attached is your sample survey with that script in the source of Q2.

File Attachment:

File Name: limesurvey...6621.lss
File Size:23 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose