Randomly show question based on previous multiple select answers

More
2 years 10 months ago #111739 by jefflanzi
jefflanzi created the topic: Randomly show question based on previous multiple select answers
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!

Please Log in to join the conversation.

More
2 years 10 months ago #111751 by tpartner
tpartner replied the topic: Randomly show question based on previous multiple select answers
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.

Please Log in to join the conversation.

More
2 years 10 months ago #111752 by Mazi
Mazi replied the topic: Randomly show question based on previous multiple select answers
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
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
2 years 10 months ago #111760 by tpartner
tpartner replied the topic: Randomly show question based on previous multiple select answers
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.

<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.
Attachments:

Please Log in to join the conversation.

More
2 years 10 months ago #111774 by jefflanzi
jefflanzi replied the topic: Randomly show question based on previous multiple select answers
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!

Please Log in to join the conversation.

More
2 years 10 months ago #111775 by tpartner
tpartner replied the topic: Randomly show question based on previous multiple select answers
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):

<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.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now