Forum Moderators: open
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
function shuffleList(ul) {
var nl = ul.getElementsByTagName("li"),
arr = [],
i,
n;
// Copy the nodeList to an array
for (i = 0; n = nl[i]; ++i) {
arr.push(n);
}
// Shuffle the array
shuffle(arr);
// Append the elements back to the ul in shuffled order
for (i = 0; i < arr.length; i++) {
ul.appendChild(arr[i]);
}
}