Fixed answer options in list+multi

More
6 months 1 week ago #167234 by Matadeleo
Matadeleo created the topic: Fixed answer options in list+multi
Hi,

I'm trying to fix answer options at the bottom of a list and while it is working for multi-choice, it isn't working for single choice (list-radio) questions.

I've attached an example survey, and I'm using the code given on the LimeSurvey workarounds page.

I've tested in version 2.50+ and 3.6.2.

File Attachment:

File Name: fixedanswe...ions.lss
File Size:22 KB
Attachments:

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

More
6 months 1 week ago #167247 by Joffm
Joffm replied the topic: Fixed answer options in list+multi
Hi,

the forums search is your friend:

here the solution:
https://www.limesurvey.org/forum/can-i-do-this-with-limesurvey/105501-partially-randomized-answers-list-radio-type-in-2-50?limitstart=0#160907


or use this from the workaround section for V2.73
https://manual.limesurvey.org/Workarounds:_Manipulating_a_survey_at_runtime_using_Javascript#Partially_Randomized_Answers_-_Multiple_Options_.26_List_.28radio.29_questions_.28Enhanced.29
<script type="text/javascript" charset="utf-8">	
 
	$(document).ready(function() {
 
		// The number of answers to be fixed at the end of the list
		var fixedAnswers = 1;
 
		// Set this to "true" if you want "Other" to be fixed in the last position
		var otherFixed = false;
 
		// Identify this question
		var qID = {QID}; 
 
		// Find the number of answers
		var ansCount = $('#question'+qID+' .answer-item').length;
		if($('#question'+qID+' input[type="text"]').length > 0) {
			ansCount = ansCount -1
		}
 
		// Place the last n answers created at the end of the list
		var fixedIndex = fixedAnswers - 1;
		for (var i=0; i<fixedAnswers; i++) {
			var answer = $('input[id^="answer"][id$="X'+qID+'s'+(ansCount-fixedIndex)+'"]');
			var answerItem = $(answer).closest('.answer-item');
			var answersList = $(answer).closest('.answers-list');
			if($('#question'+qID).hasClass('multiple-opt')) {
				answer = $('input[id^="answer"][id$="X'+qID+(ansCount-fixedIndex)+'"]');
				answerItem = $(answer).closest('.answer-item').parent();
				answersList = $(answer).closest('.subquestion-list');
			}
			$(answersList).append(answerItem);
			fixedIndex--;
		}		
 
		// Handle "Other"
		if(otherFixed == true && $('#question'+qID+' input[type="text"]').length > 0) {
			var otherAnswer = $('#question'+qID+' input[type="text"]');
			var otherAnswerItem = $(otherAnswer ).closest('.answer-item');
			var otherAnswersList = $(otherAnswer ).closest('.answers-list');
			if($('#question'+qID).hasClass('multiple-opt')) {
				otherAnswerItem = $(otherAnswer ).closest('.answer-item').parent();
				otherAnswersList = $(otherAnswer ).closest('.subquestion-list');
			}
			$(otherAnswersList).append(otherAnswerItem);
		}
	});
</script>

Joffm


Volunteers are not paid.
Not because they are worthless, but because they are priceless

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

More
6 months 1 week ago #167248 by Matadeleo
Matadeleo replied the topic: Fixed answer options in list+multi
Hi Joffm,

Thanks. I did use the search function and found my way to the 2.73 solution (which unfortunately doesn't appear to work in 3.6.2?)

But the 2.50 version worked great. Hopefully I can find a version of that code which is working at 3.6.2+!

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

More
6 months 1 week ago #167275 by tpartner
tpartner replied the topic: Fixed answer options in list+multi
Can you attach a small sample survey for 3.x including the code that you are trying?

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
6 months 1 week ago - 6 months 1 week ago #167293 by Matadeleo
Matadeleo replied the topic: Fixed answer options in list+multi
Here is a copy with version 3.6.2

Now I hope there isn't a stupid mistake I made to embarass me :P

File Attachment:

File Name: fixedanswe...V362.lss
File Size:22 KB
Attachments:
Last Edit: 6 months 1 week ago by Matadeleo.

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

More
6 months 1 week ago - 6 months 1 week ago #167325 by tpartner
tpartner replied the topic: Fixed answer options in list+multi
No mistakes at all - the HTML structure has been changed (again).

This is the script for LS version 3.x:

<script type="text/javascript" charset="utf-8">	
 
	$(document).on('ready pjax:scriptcomplete',function(){
 
		// The number of answers to be fixed at the end of the list
		var fixedAnswers = 1;
 
		// Set this to "true" if you want "Other" to be fixed in the last position
		var otherFixed = false;
 
		// Identify this question
		var qID = {QID}; 
 
		// Find the number of answers
		var ansCount = $('#question'+qID+' .answer-item').length;
		if($('#question'+qID+' input[type="text"]').length > 0) {
			ansCount = ansCount -1
		}
		console.log(ansCount);
 
		// Place the last n answers created at the end of the list
		var fixedIndex = fixedAnswers - 1;
		for (var i=0; i<fixedAnswers; i++) {
			var answer = $('input[id^="answer"][id$="X'+qID+(ansCount-fixedIndex)+'"]');
			var answerItem = $(answer).closest('.answer-item');
			var answersList = $(answer).closest('ul');
			$(answersList).append(answerItem);
			fixedIndex--;
		}		
 
		// Handle "Other"
		if(otherFixed == true && $('#question'+qID+' input[type="text"]').length > 0) {
			var otherAnswer = $('#question'+qID+' input[type="text"]');
			var otherAnswerItem = $(otherAnswer ).closest('.answer-item');
			var otherAnswersList = $(otherAnswer ).closest('ul');
			$(otherAnswersList).append(otherAnswerItem);
		}
	});
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...3199.lss
File Size:22 KB


I have updated the workaround - manual.limesurvey.org/Workarounds:_Manip...tions_.28Enhanced.29

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last Edit: 6 months 1 week ago by tpartner.
The following user(s) said Thank You: Matadeleo

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

More
6 months 4 days ago #167348 by Matadeleo
Matadeleo replied the topic: Fixed answer options in list+multi
Thanks tpartner B)

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!