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
The topic has been locked.