- Posts: 193
- Thank you received: 69
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Randomize answer options in array (not subquestions)
- elissa
- Topic Author
- Offline
- LimeSurvey Community Team
Less
More
8 years 5 months ago #128439
by elissa
Elzbieta Lesinska
LS voluntary Polish translator and supervisor
Randomize answer options in array (not subquestions) was created by elissa
Hi,
I would like to randomize answer options in an array question - not subquestions but answers options. Is there a setting or a workaround for that? I cannot find.
Regards
elisa
I would like to randomize answer options in an array question - not subquestions but answers options. Is there a setting or a workaround for that? I cannot find.
Regards
elisa
Elzbieta Lesinska
LS voluntary Polish translator and supervisor
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
8 years 5 months ago - 8 years 5 months ago #128442
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Randomize answer options in array (not subquestions)
Hi Elisa. I think this should do the trick...
1) Add this function to the end of template.js:
2) Add this script to the source of each array you want randomized:
1) Add this function to the end of template.js:
Code:
function randomArrayCols(qID){ var thisQuestion = $('#question'+qID); // Index the array columns $('table.subquestions-list tr', thisQuestion).each(function(i) { $('> *', this).each(function(i) { $(this).attr('data-index', i); }); }); if($('.noanswer-item', thisQuestion).length > 0) { $('table.subquestions-list tr > :last-child', thisQuestion).addClass('col-no-answer'); } // Create an array of the columns var columns = []; $('table.subquestions-list tr.answers-list:eq(0) td.answer-item', thisQuestion).each(function(i) { columns.push($(this).attr('data-index')); }); // A function to shuffle elements or contents of arrays (function($){ $.fn.shuffle = function() { return this.each(function(){ var items = $(this).children(); return (items.length) ? $(this).html($.shuffle(items)) : this; }); } $.shuffle = function(arr) { for( var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x ); return arr; } })(jQuery); // Shuffle the columns array columns = $.shuffle(columns); // Reposition the row elements $('table.subquestions-list tr', thisQuestion).each(function(i) { var thisRow = $(this); $(columns).each(function(i, val) { thisRow.append($('[data-index="'+val+'"]', thisRow)); thisRow.append($('.col-no-answer', thisRow)); }); }); }
2) Add this script to the source of each array you want randomized:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ randomArrayCols({QID}); }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 years 5 months ago by tpartner.
The topic has been locked.
- elissa
- Topic Author
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 193
- Thank you received: 69
8 years 5 months ago #128443
by elissa
Elzbieta Lesinska
LS voluntary Polish translator and supervisor
Replied by elissa on topic Randomize answer options in array (not subquestions)
Hi Tony,
Thank you for your help but I'm testing it and cannot make it working. Columns stay in original order every time.
Regards
elisa
Thank you for your help but I'm testing it and cannot make it working. Columns stay in original order every time.
Regards
elisa
Elzbieta Lesinska
LS voluntary Polish translator and supervisor
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
8 years 5 months ago #128444
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Randomize answer options in array (not subquestions)
0ops, sorry, I have a typo in the script for the question source. It should be this:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function(){ randomArrayCols({QID}); }); </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: elissa
The topic has been locked.
- elissa
- Topic Author
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 193
- Thank you received: 69
8 years 5 months ago #128447
by elissa
Elzbieta Lesinska
LS voluntary Polish translator and supervisor
Replied by elissa on topic Randomize answer options in array (not subquestions)
Wow, now it works perfectly. Thank you very much! That's really great help for me.
Elzbieta Lesinska
LS voluntary Polish translator and supervisor
The topic has been locked.
- zserban
- Offline
- Junior Member
Less
More
- Posts: 38
- Thank you received: 1
6 years 2 months ago #163879
by zserban
Replied by zserban on topic Randomize answer options in array (not subquestions)
Hello tpartner,
Should this work with ver. 2.7?
I am trying it without much success.
Should this work with ver. 2.7?
I am trying it without much success.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
6 years 2 months ago #163890
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Randomize answer options in array (not subquestions)
Probably not as it is - there have been many HTML changes. Can you attach a small sample survey containing only that question?
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: zserban
The topic has been locked.
- zserban
- Offline
- Junior Member
Less
More
- Posts: 38
- Thank you received: 1
6 years 2 months ago #163891
by zserban
Replied by zserban on topic Randomize answer options in array (not subquestions)
Sure, thanks for helping.
Attachments:
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
6 years 2 months ago #163896
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Randomize answer options in array (not subquestions)
This function will work for 2.73.0:
Here is your test survey back with that function and call in the source of the question:
Code:
function randomArrayCols(qID){ var thisQuestion = $('#question'+qID); // Index the array columns $('table.subquestion-list tr', thisQuestion).each(function(i) { $('> *', this).each(function(i) { $(this).attr('data-index', i); }); }); if($('.noanswer-item', thisQuestion).length > 0) { $('table.subquestion-list tr > :last-child', thisQuestion).addClass('col-no-answer'); } // Create an array of the columns var columns = []; $('table.subquestion-list tr.answers-list:eq(0) td.answer-item', thisQuestion).each(function(i) { columns.push($(this).attr('data-index')); }); // A function to shuffle elements or contents of arrays (function($){ $.fn.shuffle = function() { return this.each(function(){ var items = $(this).children(); return (items.length) ? $(this).html($.shuffle(items)) : this; }); } $.shuffle = function(arr) { for( var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x ); return arr; } })(jQuery); // Shuffle the columns array columns = $.shuffle(columns); // Reposition the row elements $('table.subquestion-list tr', thisQuestion).each(function(i) { var thisRow = $(this); $(columns).each(function(i, val) { thisRow.append($('[data-index="'+val+'"]', thisRow)); thisRow.append($('.col-no-answer', thisRow)); }); }); }
Here is your test survey back with that function and call in the source of the question:
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: zserban
The topic has been locked.
- zserban
- Offline
- Junior Member
Less
More
- Posts: 38
- Thank you received: 1
6 years 2 months ago #163897
by zserban
Replied by zserban on topic Randomize answer options in array (not subquestions)
It sure does work.
Brilliant, thank you!
Brilliant, thank you!
The topic has been locked.
- zserban
- Offline
- Junior Member
Less
More
- Posts: 38
- Thank you received: 1
5 years 9 months ago #170782
by zserban
Replied by zserban on topic Randomize answer options in array (not subquestions)
Hi tpartner,
Do you have any idea if/how to do this in Limesurvey v3.13?
I uploaded an example.
Do you have any idea if/how to do this in Limesurvey v3.13?
I uploaded an example.
Attachments:
The topic has been locked.
- zserban
- Offline
- Junior Member
Less
More
- Posts: 38
- Thank you received: 1
5 years 9 months ago #170783
by zserban
Replied by zserban on topic Randomize answer options in array (not subquestions)
Nevermind, your code still works!
All one needs to do is to add the theme code in the custom.js, rather them template.js
All one needs to do is to add the theme code in the custom.js, rather them template.js
The topic has been locked.