Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:
  • Page:
  • 1
  • 2

TOPIC: Questions based on random selection from a multiple choice

Questions based on random selection from a multiple choice 4 years 9 months ago #69263

  • tpartner
  • tpartner's Avatar
  • Online
  • LimeSurvey Team
  • Posts: 6214
  • Thank you received: 1376
  • Karma: 539
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: 4 years 9 months ago by tpartner.
The administrator has disabled public write access.

Questions based on random selection from a multiple choice 4 years 9 months ago #69313

  • uxexp
  • uxexp's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 19
  • Karma: 0
You are a genius. This worked exactly like you had suggested. I can't thank you enough.
The administrator has disabled public write access.

Questions based on random selection from a multiple choice 4 years 9 months ago #69314

  • tpartner
  • tpartner's Avatar
  • Online
  • LimeSurvey Team
  • Posts: 6214
  • Thank you received: 1376
  • Karma: 539
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: 4 years 9 months ago by tpartner.
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.215 seconds
Imprint                   Data Protection Statement                  Revocation information and revocation form