Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: Questions based on random selection from a multiple choice

Re: Questions based on random selection from a multiple choice 2 years 5 months ago #69174

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
Oops, my fault - I forgot a closing bracket.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		showOnly(2); // Modify for number of questions to show.
 
		function showOnly(shownNum) {
			$('div[id^="question"]:visible').each(function(i) {
				if(i >= shownNum) { 
					$(this).hide();
				}
			}); 
		}
 
	});
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Questions based on random selection from a multiple choice 2 years 5 months ago #69180

  • uxexp
  • uxexp's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 19
  • Karma: 0
Thanks again Tony. Still no luck. The revised script is still not working. I also see a javascript error as follows: "}" missing, on the status bar in the browser. I am attaching the survey.
Attachments:
Last Edit: 2 years 5 months ago by uxexp.
The administrator has disabled public write access.

Re: Questions based on random selection from a multiple choice 2 years 5 months ago #69209

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
Somehow the code got messed up when you pasted it. Here's the working survey. I didn't add anything to the code, all I did was clean up spacing and line breaks.

File Attachment:

File Name: limesurvey_survey_14327_TONY.lss
File Size: 43 KB
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Questions based on random selection from a multiple choice 2 years 5 months ago #69242

  • uxexp
  • uxexp's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 19
  • Karma: 0
Super. That worked!! Thanks you all very much.
The administrator has disabled public write access.

Re: Questions based on random selection from a multiple choice 2 years 5 months ago #69249

  • uxexp
  • uxexp's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 19
  • Karma: 0
Hope I am not being too greedy:)

I would like to extend this logic to randomly select groups of questions. So, I will have multiple groups (one per fruit) with same set of questions and display only 2 groups of questions based on which fruits were selected by the participant.

Would this be really difficult to achieve?

Thanks!
The administrator has disabled public write access.

Re: Questions based on random selection from a multiple choice 2 years 5 months ago #69263

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 2 years 5 months ago by tpartner.
The administrator has disabled public write access.

Re: Questions based on random selection from a multiple choice 2 years 5 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.

Re: Questions based on random selection from a multiple choice 2 years 5 months ago #69314

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 2 years 5 months ago by tpartner.
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.126 seconds
Donation Image