Welcome to the LimeSurvey Community Forum

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

Randomly pick a option from a hidden multi-choice question

  • paulfiner
  • paulfiner's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 days ago #215215 by paulfiner
I have a list of 20 brands that respondents can say whether they've heard of or not in a multi-choice question.
I am only interested in 3 of these brands which I have set up an equation to populate a hidden multi-choice question for.

If only 1 of the 3 brands is mentioned I need to use it in a following question.
If 2 or 3 brands are mentioned I need to randomly pick one of them to use later on.
If none of the brands are mentioned I need to pick a random one from all 3.

I'm using v3.25 and have seen the various workarounds in the forum and manual but am struggling to work out the logic.

Thanks,
Paul
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 days ago #215216 by tpartner
It could be done with JS. Can you attach a small sample survey (.lss file) containing only the relevant questions?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • paulfiner
  • paulfiner's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 days ago - 3 years 3 days ago #215218 by paulfiner
Hi Tony,

Here's the lss file. I just need to populate the short textfield with the chosen brand.

Cheers,
Paul
 

File Attachment:

File Name: limesurvey...7619.lss
File Size:27 KB
Last edit: 3 years 3 days ago by paulfiner.
The topic has been locked.
More
3 years 3 days ago #215220 by jelo

paulfiner wrote: I have a list of 20 brands that respondents can say whether they've heard of or not in a multi-choice question.
I am only interested in 3 of these brands which I have set up an equation to populate a hidden multi-choice question for.

If only 1 of the 3 brands is mentioned I need to use it in a following question.
If 2 or 3 brands are mentioned I need to randomly pick one of them to use later on.
If none of the brands are mentioned I need to pick a random one from all 3.

I'm using v3.25 and have seen the various workarounds in the forum and manual but am struggling to work out the logic.

Thanks,
Paul


The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 days ago #215223 by tpartner
Place this script in the source of the first multiple-choice. It will:
- place a listener on the check-boxes in that question
- randomize the checked target codes as you describe above
- load the short-text with the selected random code
- check the corresponding box in the second multiple-choice (I change the codes in that question so they match the first question)
 
 
Code:
<script type="text/javascript" data-author="Tony Partner">  
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify the questions
    var qID = '{QID}';
    var thisQuestion = $('#question'+qID);
    var textQuestion = thisQuestion.nextAll('.text-short:eq(0)');
    var nextMultiOpt = thisQuestion.nextAll('.multiple-opt:eq(0)');
    var nMOID = nextMultiOpt.attr('id').replace(/question/, '');
 
    // Identify the sub-question codes to target
    var sqCodes = [4, 5, 9];
 
    // Listener on the checkboxes
    $(':checkbox', thisQuestion).on('change', function(e) {
      // Reset the hidden questions
      $(':text', textQuestion).val('');
      $(':checkbox', nextMultiOpt).prop('checked', false).trigger('change');
 
      // Build an array of checked codes (use all if none are checked)
      var checkedCodes = [];
      $.each(sqCodes, function(i, val) {
        if($(':checkbox[id$="X'+qID+val+'"]').is(':checked')) {
          checkedCodes.push(val);
        }
      });
      var randomCodes = sqCodes;
      if(checkedCodes.length > 0) {
        randomCodes = checkedCodes;
      }
      // Randomize the codes
      shuffleArray(randomCodes);
 
      // Load the hidden questions
      var selectedCode = randomCodes[0];
      $(':text', textQuestion).val(selectedCode);
      $(':checkbox[id$="X'+nMOID+selectedCode+'"]', nextMultiOpt).prop('checked', true).trigger('change');
    });  
  });
 
  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...9(1).lss
File Size:30 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: paulfiner
The topic has been locked.
  • paulfiner
  • paulfiner's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 days ago #215224 by paulfiner
Hi Tony,

Thank you so much for helping out.
I’ve left work now but will implement this in the morning.

Cheers
Paul
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 days ago #215225 by Joffm
Hi,

Of course Tony provides the awesome javascript.
As I am not able to write one line of javascript by myself, my speciality are equations.

This is just to show that it is possible with only three equations.
L'art pour l'art
 

File Attachment:

File Name: limesurvey... (1).lss
File Size:31 KB


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: paulfiner
The topic has been locked.
  • paulfiner
  • paulfiner's Avatar Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
3 years 3 days ago #215230 by paulfiner
Hi Joffm,

Thanks for contributing to my problem and that's a neat solution for sure.
I've got some studying to do!

Cheers,
Paul
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose