Randomize answers except a few at bottom

More
2 months 3 days ago #157565 by ARei
ARei created the topic: Randomize answers except a few at bottom
Hello!

Unfortunately not having any knowledge in JavaScript, I'd appreciate your assistance with the following:

I want to randomize a set of answers, but keeping a specific amount fixed at the bottom. Most often there are two answer options ("don't know" and "no answer", whereas the "no answer"-option isn't the default option but programmed manually). Sometimes there is an additional "none of the above". Ideally they should all show up under the "Other"-field.

I have found the following script from this thread:
script type=textjavascript charset=utf-8
	$(document).ready(function() {
 
		 Identify this question
		var qID = {QID};
		var thisQuestion = $('#question'+qID);
 
		 Define the sub-question code to be placed last
		var lastItem = 'SQ004';
 
		 Move that item to the end of the list
		$('.question-item[id$=X'+qID+lastItem+']', thisQuestion).addClass('last-item').parent().appendTo($('.subquestion-list', thisQuestion));
	});
script

Interestingly, this was the only code that worked for us. It was also a rather simple code, which is important to us as it should be easy and quick to modify and ideally easy to understand for multiple people.

Of course, this solution is only for one answer option, not for 2 or 3 and, so I understand, for multiple choice questions.
We would need a code that works for both array and multiple choice options.


My question is: is it possible to modify this code so it works for 2 or optionally 3 options? And if so, how?


Thanks in advance for the answers!

Please Log in to join the conversation.

More
2 months 3 days ago #157567 by tpartner
tpartner replied the topic: Randomize answers except a few at bottom
Assuming you are using the latest LimeSurvey version, this will fix a number of sub-questions to the bottom for both multi-choice and array type questions.

<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {
 
		// Identify this question
		var qID = {QID};
		var thisQuestion = $('#question'+qID);
 
		// Define the sub-question codes to be placed last
		var lastItems = ['SQ006', 'SQ007', 'SQ008'];
 
		// Loop through those sub-question codes 
		$.each(lastItems, function(i, val) {
			// Move that item to the end of the list
			// Multi-choice question
			if($(thisQuestion).hasClass('multiple-opt')) {
				$('.question-item[id$=X'+qID+val+']', thisQuestion).parent().appendTo($('.subquestion-list', thisQuestion));
			}
			// Array question
			if($(thisQuestion).hasClass('array-flexible-row')) {
				$('.answers-list[id$=X'+qID+val+']', thisQuestion).appendTo($('table.subquestion-list', thisQuestion));
			}
		});	
	});
</script>

Sample survey attached

File Attachment:

File Name: limesurvey...8-14.lss
File Size:25 KB

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

Please Log in to join the conversation.

More
2 months 3 days ago #157575 by ARei
ARei replied the topic: Randomize answers except a few at bottom
Thank you very much for the help! This works very well.

I tried to figure out on my own how to do the same for a List (radio) but couldn't figure out how to modify the last line of the code. And what the class is called exactly for the IF-command.

If you could help me once more I'd greatly appreciate it!

Please Log in to join the conversation.

More
2 months 2 days ago #157589 by tpartner
tpartner replied the topic: Randomize answers except a few at bottom
This should work for list-radio questions as well:

<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {
 
		// Identify this question
		var qID = {QID};
		var thisQuestion = $('#question'+qID);
 
		// Define the sub-question codes to be placed last
		var lastItems = ['SQ006', 'SQ007', 'SQ008'];
 
		// Loop through those sub-question codes 
		$.each(lastItems, function(i, val) {
			// Move that item to the end of the list
			// Multi-choice question
			if($(thisQuestion).hasClass('multiple-opt')) {
				$('.question-item[id$=X'+qID+val+']', thisQuestion).parent().appendTo($('.subquestion-list', thisQuestion));
			}
			// Array question
			if($(thisQuestion).hasClass('array-flexible-row')) {
				$('.answers-list[id$=X'+qID+val+']', thisQuestion).appendTo($('table.subquestion-list', thisQuestion));
			}
			// List-radio question
			if($(thisQuestion).hasClass('list-radio')) {
				$('.answer-item[id$=X'+qID+val+']', thisQuestion).appendTo($('.answers-list', thisQuestion));
			}
		});	
	});
</script>

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: ARei

Please Log in to join the conversation.

More
2 months 4 hours ago #157703 by ARei
ARei replied the topic: Randomize answers except a few at bottom
Thank you very much for the help! Works great.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now