JS for selecting two random responses from a multiple choice question

More
3 years 5 months ago - 3 years 5 months ago #143481 by tpartner
Yep, that would be like this:

<script type="text/javascript" charset="utf-8">		
$(document).ready(function() {	
 
		// Identify the questions
		var thisQuestion = $('#question{QID}');
		var thisQuestionID = {QID};
		var qHidden = thisQuestion.nextAll('.multiple-short-txt:eq(0)');
		var hiddenInput1 = $('input.text:eq(0)', qHidden);
		var hiddenInput2 = $('input.text:eq(1)', qHidden);
		var qHidden2 = thisQuestion.nextAll('.multiple-opt:eq(0)');
		var qHidden2ID = qHidden2.attr('id').replace(/question/, '');
 
		// Hide the control questions
		qHidden.hide();
		qHidden2.hide();
 
		// Listener on the checkboxes
		$('input.checkbox', thisQuestion).on('change', function(e) {
			handleChecked();
		});
 
		function handleChecked() {
			// Build an array of checked sub-question codes
			var checkedAnswers = [];
			$('input.checkbox:checked', thisQuestion).each(function(i) {
				var thisSQCode = $(this).attr('id').split('X'+thisQuestionID)[1];
				checkedAnswers.push(thisSQCode);
			});
 
			// Shuffle the array
			shuffleArray(checkedAnswers);
 
			// Reset the hidden multi-choice
			$('input.checkbox', qHidden2).each(function(i) {
				$(this).prop('checked', false);
				$(this).nextAll('input:hidden:eq(0)').val('');
			});
 
			// Load the hidden questions with 2 random items from the visible multi-choice
			$(hiddenInput1).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[0]+'"] .label-text').text()));
			$('input.checkbox[id$="X'+qHidden2ID+checkedAnswers[0]+'"]', qHidden2).trigger('click');
			if(checkedAnswers.length > 1) {
				$(hiddenInput2).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[1]+'"] .label-text').text()));
				$('input.checkbox[id$="X'+qHidden2ID+checkedAnswers[1]+'"]', qHidden2).trigger('click');
			}
			else {
				$(hiddenInput2).val('');
			}
 
			// Fire Expression Manager
			checkconditions(hiddenInput1.value, hiddenInput1.name, hiddenInput1.type);
			checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type);
			$('input.checkbox', qHidden2).each(function(i) {
				checkconditions(this.value, this.name, this.type);
			});
		}
    });
 
	function shuffleArray(array) {
		for (var i = array.length - 1; i > 0; i--) {
			var j = Math.floor(Math.random() * (i + 1));
			var temp = array[i];
			array[i] = array[j];
			array[j] = temp;
		}
		return array;
	}
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...21-2.lss
File Size:24 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com
Attachments:
Last edit: 3 years 5 months ago by tpartner.

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

More
3 years 5 months ago #143487 by dieterdubinsky
Tony, you are a genius! I hope this was not too much trouble :-). Everything is working perfectly, I couldn't ask for more.

I hope this helps others, too. Selecting random responses from a list of "known" items is very common in e.g. brand studies - so it is a frequently used feature in a lot of surveys.

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

More
1 month 5 days ago #194635 by arnabbhuyan
Hi,
I tried to use the .lss file to test. But unfortunately it seems the code is not working. Even I tried to unhide "qhidden" and the values are not getting punched in the question. Can you please help me on this.

Thanks,
AB
Attachments:

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

More
1 month 5 days ago #194638 by tpartner
I'm not surprised it doesn't work after more than 3 years.

What LimeSurvey version are you using?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com
The following user(s) said Thank You: DenisChenu

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

More
1 month 5 days ago #194641 by arnabbhuyan
Hi,
Version 3.16.1+190314

Thanks,
AB

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

More
1 month 5 days ago #194648 by tpartner
This will work in versions 3.x and 4.x:

<script type="text/javascript" charset="utf-8">		
	$(document).ready(function() {	
 
		// Identify the questions
		var thisQuestion = $('#question{QID}');
		var thisQuestionID = {QID};
		var qHidden = thisQuestion.nextAll('.multiple-short-txt:eq(0)');
		var hiddenInput1 = $('input:text:eq(0)', qHidden);
		var hiddenInput2 = $('input:text:eq(1)', qHidden);
		var qHidden2 = thisQuestion.nextAll('.multiple-opt:eq(0)');
		var qHidden2ID = qHidden2.attr('id').replace(/question/, '');
 
		// Hide the control questions
		qHidden.hide();
		qHidden2.hide();
 
		// Listener on the radios
		$('input:checkbox', thisQuestion).on('change', function(e) {
			handleChecked();
		});
 
		function handleChecked() {
			// Build an array of checked sub-question codes
			var checkedAnswers = [];
			$('input:checkbox:checked', thisQuestion).each(function(i) {
				var thisSQCode = $(this).attr('id').split('X'+thisQuestionID)[1];
				checkedAnswers.push(thisSQCode);
			});
 
			// Shuffle the array
			shuffleArray(checkedAnswers);
 
			// Reset the hidden multi-choice
			$('input:checkbox', qHidden2).each(function(i) {
				$(this).prop('checked', false);
				$(this).nextAll('input:hidden:eq(0)').val('');
			});
 
			// Load the hidden questions with 2 random items from the array
			$(hiddenInput1).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[0]+'"] .checkbox-label').text()));
			$('input:checkbox[id$="X'+qHidden2ID+checkedAnswers[0]+'"]', qHidden2).trigger('click');
			if(checkedAnswers.length > 1) {
				$(hiddenInput2).val($.trim($('.question-item[id$="X'+thisQuestionID+checkedAnswers[1]+'"] .checkbox-label').text()));
				$('input:checkbox[id$="X'+qHidden2ID+checkedAnswers[1]+'"]', qHidden2).trigger('click');
			}
			else {
				$(hiddenInput2).val('');
			}
 
			// Fire Expression Manager
			checkconditions(hiddenInput1.value, hiddenInput1.name, hiddenInput1.type);
			checkconditions(hiddenInput2.value, hiddenInput2.name, hiddenInput2.type);
			$('input:checkbox', qHidden2).each(function(i) {
				checkconditions(this.value, this.name, this.type);
			});
		}
    });
 
	function shuffleArray(array) {
		for (var i = array.length - 1; i > 0; i--) {
			var j = Math.floor(Math.random() * (i + 1));
			var temp = array[i];
			array[i] = array[j];
			array[j] = temp;
		}
		return array;
	}
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...2-27.lss
File Size:26 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com
Attachments:
The following user(s) said Thank You: DenisChenu

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

More
4 weeks 1 day ago #194845 by arnabbhuyan
Thanks Man!!
It works.

Can you also let me know which code will work for 2.05 shared in this forum?
And what changes you made in the code to work for 3.0 or above?

Thanks,
AB

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

More
4 weeks 1 day ago #194852 by tpartner
Sorry, I don't have time for that. You can search the forum and compare scripts as easily as I can.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

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

More
4 weeks 1 day ago #194859 by arnabbhuyan

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!