- Posts: 11
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Can I select a random answer from a multiple choice question
- elenas
- Topic Author
- Offline
- New Member
Less
More
3 years 3 months ago #210879
by elenas
Can I select a random answer from a multiple choice question was created by elenas
Hi,
i have to randomly select one of the answers given in a previous multiple choice question.
I've found this script in another post but it didn't work in version 2.06+ (without "$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).prop('checked', true);
$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');" , last two commands )
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
// Identify this question
var qID = {QID};
var thisQuestion = $('#question'+qID);
// 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, arr = arr[j], arr[j] = x
);
return arr;
}
})(jQuery);
// Build an array of codes of all visible sub-questions
var sqCodes = [];
$('li.answer-item:not(.ls-irrelevant)', thisQuestion).each(function(i) {
var thisCode = $(this).attr('id').split('X'+qID)[1];
sqCodes.push(thisCode);
});
// Shuffle the sub-question codes array
sqCodes = $.shuffle(sqCodes);
// Check the first two sub-question codes
$('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).prop('checked', true);
$('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');
$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).prop('checked', true);
$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');
});
</script>
Thanks in advance
i have to randomly select one of the answers given in a previous multiple choice question.
I've found this script in another post but it didn't work in version 2.06+ (without "$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).prop('checked', true);
$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');" , last two commands )
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
// Identify this question
var qID = {QID};
var thisQuestion = $('#question'+qID);
// 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, arr = arr[j], arr[j] = x
);
return arr;
}
})(jQuery);
// Build an array of codes of all visible sub-questions
var sqCodes = [];
$('li.answer-item:not(.ls-irrelevant)', thisQuestion).each(function(i) {
var thisCode = $(this).attr('id').split('X'+qID)[1];
sqCodes.push(thisCode);
});
// Shuffle the sub-question codes array
sqCodes = $.shuffle(sqCodes);
// Check the first two sub-question codes
$('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).prop('checked', true);
$('input[type="checkbox"][id$="X'+qID+sqCodes[0]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');
$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).prop('checked', true);
$('input[type="checkbox"][id$="X'+qID+sqCodes[1]+'"]', thisQuestion).nextAll('input:hidden:eq(0)').val('Y');
});
</script>
Thanks in advance
The topic has been locked.
- jelo
- Offline
- Platinum Member
Less
More
- Posts: 5033
- Thank you received: 1257
3 years 3 months ago #210881
by jelo
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Replied by jelo on topic Can I select a random answer from a multiple choice question
You might create a demosurvey with just a few questions and attach the LSS-export here. Personally it's a pity that LimeSurvey never considered adding dynamic and static list support to use given answers as lists and do nice things via ExpressionScript with it.
Feature request: 11688: List building - static lists - dynamic lists
bugs.limesurvey.org/view.php?id=11688
Feature request: 11688: List building - static lists - dynamic lists
bugs.limesurvey.org/view.php?id=11688
The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12937
- Thank you received: 3979
3 years 3 months ago #210890
by Joffm
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Can I select a random answer from a multiple choice question
Does it mean, you work with 2.06?but it didn't work in version 2.06+
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
- elenas
- Topic Author
- Offline
- New Member
Less
More
- Posts: 11
- Thank you received: 0
3 years 3 months ago #210891
by elenas
Replied by elenas on topic Can I select a random answer from a multiple choice question
yes
The topic has been locked.
- holch
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11660
- Thank you received: 2742
3 years 3 months ago #210900
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on topic Can I select a random answer from a multiple choice question
You shouldn't.Does it mean, you work with 2.06?
yes
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
The topic has been locked.