Randomize Answers in Array by Column

More
1 year 1 month ago - 1 year 1 month ago #158317 by teracomp
teracomp created the topic: Randomize Answers in Array by Column
I have a survey with 30 questions that are Array by Column. The columns are "Most" and "Least" and each has 4 answer options from which to choose Most or Least. (see screenshot). I have Javascript running (thanks to Tony Partner some months ago) that prevents the user from selecting both Most and Least for the same item. :)

Is there a method that will randomize the 4 answer options?

Dave Phillips
Attachments:
Last Edit: 1 year 1 month ago by teracomp.

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

More
1 year 1 month ago #158337 by tpartner
tpartner replied the topic: Randomize Answers in Array by Column
Can you attach a sample survey containing only that question and your script?

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
1 year 1 month ago #158339 by teracomp
teracomp replied the topic: Randomize Answers in Array by Column
Sample survey is attached with 4 items.
The template that I'm using includes a Javascript file with the code you (tpartner) provided some time ago. This ensures participants can't select Most and Least at the same time:
$(document).ready(function() {    
 
// Identify the questions -- FOR DISC
  var thisQuestion = $('[id^=question]');
  // Listener on the radios
  $('input.radio', thisQuestion).on('click', function(e) {
    $('input[type="radio"]', thisQuestion).prop('disabled', false);
    $('input.radio:checked', thisQuestion).each(function(i) {
      $(this).closest('tr.answers-list').find('input[type="radio"]').not(this).prop('disabled', true);
    });
  });
 
   // Initial settings
  $('input.radio:checked', thisQuestion).each(function(i) {
    $(this).closest('tr.answers-list').find('input[type="radio"]').not(this).prop('disabled', true);
  });
});

Dave Phillips
Attachments:

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

More
1 year 1 month ago - 1 year 1 month ago #158346 by tpartner
tpartner replied the topic: Randomize Answers in Array by Column
This script, when placed in the question text, will randomize the rows of an array-columns question:

<script type="text/javascript" charset="utf-8">
	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;
	}
 
	$(document).on('ready pjax:complete',function() {
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		var elements = $('tr.answers-list', thisQuestion).detach().toArray();
		shuffleArray(elements);
		$('tr.answers-list', thisQuestion).remove();
		$.each(elements, function(i, el) {
			$('table.subquestion-list tbody:eq(0)', thisQuestion).append(el);
		});
	});
</script>

Attached is a sample survey:

File Attachment:

File Name: limesurvey...5333.lss
File Size:18 KB

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

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

More
1 year 1 month ago #158348 by teracomp
teracomp replied the topic: Randomize Answers in Array by Column
Wow! Thanks for your solution...once again! Really appreciate the elegant Javascript solution.

Dave Phillips

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

More
1 year 1 month ago #158354 by teracomp
teracomp replied the topic: Randomize Answers in Array by Column
Since I present the items one at a time, I was able to add the code to my custom js file for this survey by tweaking one line:
// var thisQuestion = $('#question{QID}');
var thisQuestion = $('[id^=question]');

Nice to have the code in one place and not in each question. :)

Thanks again tpartner!

Dave Phillips

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

More
1 year 1 month ago #158356 by tpartner
tpartner replied the topic: Randomize Answers in Array by Column
Yep, but your selector targets all question types, I would be more specific:

var thisQuestion = $('.array-flexible-column:eq(0)');

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

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

More
1 year 1 month ago #158359 by teracomp
teracomp replied the topic: Randomize Answers in Array by Column
Thank you!

Dave Phillips

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

More
7 months 1 week ago #165330 by stephanied
stephanied replied the topic: Randomize Answers in Array by Column
In version 2.73.1, I would like to take this one step further and partially randomize. I tried to combine the shuffle array script with a functioning script that partially randomizes an array question. The shuffle array script works properly and when the other code is added, all the answers still randomize. Any suggestions on how to fix this?
<script type="text/javascript" charset="utf-8">
	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;
	}
 
	$(document).on('ready pjax:complete',function() {
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		var elements = $('tr.answers-list', thisQuestion).detach().toArray();
		shuffleArray(elements);
		$('tr.answers-list', thisQuestion).remove();
		$.each(elements, function(i, el) {
			$('table.subquestion-list tbody:eq(0)', thisQuestion).append(el);
		});
 
// The answer code to place in the last position
		var fixedCode = 'A4';
 
// Move the "fixed" row to the end
		$('table.subquestion-list tbody:eq(0), table.subquestions-list tbody:eq(0)', thisQuestion).append($('tr[id$="X'+{QID}+fixedCode +'"]'));
 
// Fix up the array row background colours
		$('table.subquestion-list tbody tr, table.subquestions-list tbody tr', thisQuestion).each(function(i){
			$(this).removeClass('array1 array2').addClass('array'+(2-(i%2)));
		});
	});
</script>
I've also tried to change "table.subquestion-list" to "table.answers-list"
Can someone please tell me which one is more appropriate for array-columns and why?

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

More
7 months 1 week ago #165348 by tpartner
tpartner replied the topic: Randomize Answers in Array by Column
Try this:

<script type="text/javascript" charset="utf-8">
	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;
	}
 
	$(document).on('ready pjax:complete',function() {
 
		// Identify this question
		var thisQuestion = $('#question{QID}');
 
		var elements = $('tr.answers-list', thisQuestion).detach().toArray();
		shuffleArray(elements);
		$('tr.answers-list', thisQuestion).remove();
		$.each(elements, function(i, el) {
			$('table.subquestion-list tbody:eq(0)', thisQuestion).append(el);
		});
 
		// The answer code to place in the last position
		var fixedCode = 'A4';
 
		// Move the "fixed" row to the end
		$('table.subquestion-list tbody:eq(0), table.subquestions-list tbody:eq(0)', thisQuestion).append($('tr[id="javatbd'+fixedCode +'"]'));
	});
</script>

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
7 months 6 days ago #165428 by stephanied
stephanied replied the topic: Randomize Answers in Array by Column
This works perfectly!

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!