Welcome to the LimeSurvey Community Forum

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

Randomly show question based on previous multiple select answers

  • jefflanzi
  • jefflanzi's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 7 months ago #111739 by jefflanzi
Hello,

I'm trying to create a follow-up question that randomly chooses from one of the selected answers to a previous multiple choice question. The respondent might select all the answers in the multiple choice question, but I only want to have them do the follow-up question for one of the answers, chosen at random.

e.g.

Q1: What fruits have you eaten? [Multiple select]
A1: Apple
A2: Orange
A3: Banana
A4: Grape
A5: Peach
A6: Strawberry

Q2: Please rate your satisfaction with your last [random answer marked Yes from Q1]

Does anyone have a suggestion on how this can be accomplished? Any help or nudges in the right direction would be greatly appreciated.

Thank you!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 6 months ago #111751 by tpartner
Are the questions on the same or separate pages?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
9 years 6 months ago #111752 by Mazi
There are no existing features for this but if questions are on the same page, Tony might help out with some JavaScript magic :-)

Best regards/Beste Grüße,
Dr. Marcel Minke
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 6 months ago #111760 by tpartner
If the questions are on the same page...

1) Directly after Q1, insert a copy of Q1 which has random answer order and is filtered by Q1.

2) Filter Q2 by the hidden question.

3) Add the following script to the hidden question. This will put a listener on Q1 so, when changes are detected, only the first visible checkbox of the hidden question is checked. This effectively displays a single random row of Q2, filtered on Q1 while accommodating a "mandatory" setting on Q2.

Code:
<script type="text/javascript" charset="utf-8">  
    $(document).ready(function(){
 
    // Identify the questions
    var qHidden = $('#question{QID}');
    var q1 = $(qHidden).prevAll('.multiple-opt:eq(0)');
 
    // Hide the hidden question
    $(qHidden).css({
      'position': 'absolute',
      'left': '-999em',
      'height': '0'
    })
 
    // Listener on Q1
    $('input.checkbox', q1).click(function(event){
      // Reset the hidden question
      $('input.checkbox', qHidden).each(function(i) {
        $(this).prop('checked', false);
        checkconditions($(this).attr('value'), $(this).attr('name'), $(this).attr('type'));
      });      
 
      // Check the first visible box in the hidden question
      var firstItem = $('li.question-item:visible:eq(0) input.checkbox', qHidden);
      $(firstItem).click();
    });
  });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...8-19.lss
File Size:29 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.
  • jefflanzi
  • jefflanzi's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
9 years 6 months ago #111774 by jefflanzi
Wow that looks great thanks! The questions are currently on separate pages, but I'll do some rearranging to give that JavaScript a shot.

My other idea was to create a separate question for each answer option and place them in their own randomized question group. Then use relevance equations to only show each question if it's corresponding box was checked in Q1, and if no other questions in the group have been answered yet. This seems like it would allow me to keep the questions on separate pages, but I'm still going through the EM documentation to get this figured out.

I also tried using an equation question to randomly determine which question to display, but I couldn't figure out how to save the checked boxes from the previous question into some kind of array or list variable and select only from those.

I'll keep working on all these solutions and let you guys know how it turns out. Thank you again for the help!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
9 years 6 months ago #111775 by tpartner
If on separate pages, you should be able to follow the instructions above but place the hidden question on the same page as Q2 and use this script instead (not tested):

Code:
<script type="text/javascript" charset="utf-8">  
    $(document).ready(function(){
 
    // Identify the questions
    var qHidden = $('#question{QID}');
 
    // Hide the hidden question
    $(qHidden).css({
      'position': 'absolute',
      'left': '-999em',
      'height': '0'
    })
 
    // Reset the hidden question
    $('input.checkbox', qHidden).each(function(i) {
      $(this).prop('checked', false);
      checkconditions($(this).attr('value'), $(this).attr('name'), $(this).attr('type'));
    });      
 
    // Check the first visible box in the hidden question
    var firstItem = $('li.question-item:visible:eq(0) input.checkbox', qHidden);
    $(firstItem).click();
  });
</script>

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