Hi Elisa. I think this should do the trick...
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));
});
});
}