random auto select options in multiple choice question

More
5 months 3 weeks ago #168988 by agustinface
agustinface created the topic: random auto select options in multiple choice question
Hi All
This is what I need:
I have a first multiple choice question and in the next I want to randomly select (javascript?) two of the options you have chosen in the previous question.
I am using version 3.7.2 of limesurvey.
I attach an example survey.
Can somebody help me? Thank you very much!
I used some examples that I found in the forum but none worked.
Attachments:

Please Log in or Create an account to join the conversation.

More
5 months 3 weeks ago #168991 by tpartner
tpartner replied the topic: random auto select options in multiple choice question
Can the question be on separate pages so the filtering doesn't mess with JavaScript?

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

Please Log in or Create an account to join the conversation.

More
5 months 3 weeks ago #168994 by agustinface
agustinface replied the topic: random auto select options in multiple choice question
Yes, the question can be on separate pages.

Please Log in or Create an account to join the conversation.

More
5 months 3 weeks ago #168996 by tpartner
tpartner replied the topic: random auto select options in multiple choice question
Okay, I am travelling today but will provide a JavaScript snippet tomorrow.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: agustinface

Please Log in or Create an account to join the conversation.

More
5 months 3 weeks ago #169021 by tpartner
tpartner replied the topic: random auto select options in multiple choice question
This script will randomly select two visible check-boxes.

<script type="text/javascript" charset="utf-8">
 
	$(document).on('ready pjax:scriptcomplete',function(){
		// Identify this question
		var qID = {QID};
		var thisQuestion = $('#question'+qID);
 
		// A function to shuffle elements or contents of arrays
		(function($){
			$.fn.shuffle = function() {
				return this.each(function(){
					var items = $(this).children();
					return (items.length)
						? $(this).html($.shuffle(items))
						: this;
				});
			}
 
			$.shuffle = function(arr) {
				for(
					var j, x, i = arr.length; i;
					j = parseInt(Math.random() * i),
					x = arr[--i], arr[i] = arr[j], arr[j] = x
				);
				return arr;
 
			}
		})(jQuery);
 
		// Build an array of codes of all visible sub-questions
		var sqCodes = [];
		$('li.answer-item:not(.ls-irrelevant)', thisQuestion).each(function(i) {
			var thisCode = $(this).attr('id').split('X'+qID)[1];
			sqCodes.push(thisCode);
		});
 
		// Shuffle the sub-question codes array
		sqCodes = $.shuffle(sqCodes);
 
		// Check the first two sub-question codes
		$('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).prop('checked', true);
		$('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');
		$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).prop('checked', true);
		$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');
	});	
</script>

Attached is your sample survey with that script in the source of Q2.

File Attachment:

File Name: limesurvey...6621.lss
File Size:23 KB

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!