Randomize Answers in Array by Column

More
3 months 1 week ago - 3 months 1 week 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: 3 months 1 week ago by teracomp.

Please Log in to join the conversation.

More
3 months 1 week 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 to join the conversation.

More
3 months 1 week 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 to join the conversation.

More
3 months 1 week ago - 3 months 1 week 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: 3 months 1 week ago by tpartner.
The following user(s) said Thank You: teracomp

Please Log in to join the conversation.

More
3 months 1 week 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 to join the conversation.

More
3 months 1 week 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 to join the conversation.

More
3 months 1 week 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 to join the conversation.

More
3 months 1 week ago #158359 by teracomp
teracomp replied the topic: Randomize Answers in Array by Column
Thank you!

Dave Phillips

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!