Check out the LimeSurvey source code on GitHub!

Questions based on random selection from a multiple choice

More
5 years 3 months ago - 5 years 3 months ago #69263 by tpartner
I think a way to handle that would be to:

1) Add two short-text questions to group 1 (we'll hide them with JavaScript)

2) On submit of group 1...
- add all selected fruits to an array
- randomize the array
- load the hidden questions with the first two array items

3) Use conditions to only display the questions in a fruit-group if the answer to hidden1 or hidden2 is that fruit.

Note that the order of the groups would not be randomized but which ones displayed would be.

Here's an example of code that will hide and load the hidden questions. Replace "MM" with the multiple-options ID, "H1" with the first short-text ID and "H2" with the second short-text ID.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		var multiOptID = MM;
		var hidden1ID = H1;
		var hidden2ID = H2;
 
		// Hide hidden questions
		$('#question'+hidden1ID+',#question'+hidden2ID+'').hide();
 
		// Interrupt next/submit function 
		$('#movenextbtn, #movesubmitbtn').click(function(){
 
			// Load all checked multi-opt answers into an array
			var answersArr = new Array();
			$('#question'+multiOptID+' input.checkbox:checked').each(function(){
				answersArr.push($(this).siblings('label').text());
			});
 
			// Shuffle the array
			function shuffle(o){
				for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
				return o;
			}			
			shuffle(answersArr);
 
			// Load the first two array items into the hidden questions
			$('#question'+hidden1ID+' input.text').val('').val(answersArr[0]);
			$('#question'+hidden2ID+' input.text').val('').val(answersArr[1]);
 
			return true;
		});
 
	});
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 3 months ago by tpartner.

Please Log in to join the conversation.

More
5 years 3 months ago #69313 by uxexp
You are a genius. This worked exactly like you had suggested. I can't thank you enough.

Please Log in to join the conversation.

More
5 years 3 months ago - 5 years 3 months ago #69314 by tpartner
You're welcome.

There are many ways to help improve LimeSurvey and keep it free. Please consider a donation or explore other ways to help - docs.limesurvey.org/tiki-index.php?page=Help+us%3A+Tasks

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 3 months ago by tpartner.

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form