Welcome to the LimeSurvey Community Forum

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

JS for selecting two random responses from a multiple choice question

More
8 years 10 months ago - 8 years 10 months ago #143481 by tpartner
Yep, that would be like this:

Code:
<script type="text/javascript" charset="utf-8">    
$(document).ready(function() {  
 
    // Identify the questions
    var thisQuestion = $('#question{QID}');
    var thisQuestionID = {QID};
    var qHidden = thisQuestion.nextAll('.multiple-short-txt:eq(0)');
    var hiddenInput1 = $('input.text:eq(0)', qHidden);
    var hiddenInput2 = $('input.text:eq(1)', qHidden);
    var qHidden2 = thisQuestion.nextAll('.multiple-opt:eq(0)');
    var qHidden2ID = qHidden2.attr('id').replace(/question/, '');
 
    // Hide the control questions
    qHidden.hide();
    qHidden2.hide();
 
    // Listener on the checkboxes
    $('input.checkbox', thisQuestion).on('change', function(e) {
      handleChecked();
    });
 
    function handleChecked() {
      // Build an array of checked sub-question codes
      var checkedAnswers = [];
      $('input.checkbox:checked', thisQuestion).each(function(i) {
        var thisSQCode = $(this).attr('id').split('X'+thisQuestionID)[1];
        checkedAnswers.push(thisSQCode);
      });
 
      // Shuffle the array
      shuffleArray(checkedAnswers);
 
      // Reset the hidden multi-choice
      $('input.checkbox', qHidden2).each(function(i) {
        $(this).prop('checked', false);
        $(this).nextAll('input:hidden:eq(0)').val('');
      });
 
      // Load the hidden questions with 2 random items from the visible multi-choice
      $(hiddenInput1).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[0]+'"] .label-text').text()));
      $('input.checkbox[id$="X'+qHidden2ID+checkedAnswers[0]+'"]', qHidden2).trigger('click');
      if(checkedAnswers.length > 1) {
        $(hiddenInput2).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[1]+'"] .label-text').text()));
        $('input.checkbox[id$="X'+qHidden2ID+checkedAnswers[1]+'"]', qHidden2).trigger('click');
      }
      else {
        $(hiddenInput2).val('');
      }
 
      // Fire Expression Manager
      checkconditions(hiddenInput1.value, hiddenInput1.name, hiddenInput1.type);
      checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type);
      $('input.checkbox', qHidden2).each(function(i) {
        checkconditions(this.value, this.name, this.type);
      });
    }
    });
 
  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;
  }
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...21-2.lss
File Size:24.23 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 years 10 months ago by tpartner.
The topic has been locked.
More
8 years 10 months ago #143487 by dieterdubinsky
Tony, you are a genius! I hope this was not too much trouble :-). Everything is working perfectly, I couldn't ask for more.

I hope this helps others, too. Selecting random responses from a list of "known" items is very common in e.g. brand studies - so it is a frequently used feature in a lot of surveys.
The topic has been locked.
More
5 years 6 months ago #194635 by arnabbhuyan
Hi,
I tried to use the .lss file to test. But unfortunately it seems the code is not working. Even I tried to unhide "qhidden" and the values are not getting punched in the question. Can you please help me on this.

Thanks,
AB
The topic has been locked.
More
5 years 6 months ago #194638 by tpartner
I'm not surprised it doesn't work after more than 3 years.

What LimeSurvey version are you using?

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: DenisChenu
The topic has been locked.
More
5 years 6 months ago #194641 by arnabbhuyan
Hi,
Version 3.16.1+190314

Thanks,
AB
The topic has been locked.
More
5 years 6 months ago #194648 by tpartner
This will work in versions 3.x and 4.x:

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function() {  
 
    // Identify the questions
    var thisQuestion = $('#question{QID}');
    var thisQuestionID = {QID};
    var qHidden = thisQuestion.nextAll('.multiple-short-txt:eq(0)');
    var hiddenInput1 = $('input:text:eq(0)', qHidden);
    var hiddenInput2 = $('input:text:eq(1)', qHidden);
    var qHidden2 = thisQuestion.nextAll('.multiple-opt:eq(0)');
    var qHidden2ID = qHidden2.attr('id').replace(/question/, '');
 
    // Hide the control questions
    qHidden.hide();
    qHidden2.hide();
 
    // Listener on the radios
    $('input:checkbox', thisQuestion).on('change', function(e) {
      handleChecked();
    });
 
    function handleChecked() {
      // Build an array of checked sub-question codes
      var checkedAnswers = [];
      $('input:checkbox:checked', thisQuestion).each(function(i) {
        var thisSQCode = $(this).attr('id').split('X'+thisQuestionID)[1];
        checkedAnswers.push(thisSQCode);
      });
 
      // Shuffle the array
      shuffleArray(checkedAnswers);
 
      // Reset the hidden multi-choice
      $('input:checkbox', qHidden2).each(function(i) {
        $(this).prop('checked', false);
        $(this).nextAll('input:hidden:eq(0)').val('');
      });
 
      // Load the hidden questions with 2 random items from the array
      $(hiddenInput1).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[0]+'"] .checkbox-label').text()));
      $('input:checkbox[id$="X'+qHidden2ID+checkedAnswers[0]+'"]', qHidden2).trigger('click');
      if(checkedAnswers.length > 1) {
        $(hiddenInput2).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[1]+'"] .checkbox-label').text()));
        $('input:checkbox[id$="X'+qHidden2ID+checkedAnswers[1]+'"]', qHidden2).trigger('click');
      }
      else {
        $(hiddenInput2).val('');
      }
 
      // Fire Expression Manager
      checkconditions(hiddenInput1.value, hiddenInput1.name, hiddenInput1.type);
      checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type);
      $('input:checkbox', qHidden2).each(function(i) {
        checkconditions(this.value, this.name, this.type);
      });
    }
    });
 
  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;
  }
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...2-27.lss
File Size:25.53 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: DenisChenu
The topic has been locked.
More
5 years 6 months ago #194845 by arnabbhuyan
Thanks Man!!
It works.

Can you also let me know which code will work for 2.05 shared in this forum?
And what changes you made in the code to work for 3.0 or above?

Thanks,
AB
The topic has been locked.
More
5 years 6 months ago #194852 by tpartner
Sorry, I don't have time for that. You can search the forum and compare scripts as easily as I can.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
5 years 6 months ago #194859 by arnabbhuyan
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose