La riorganizzazione casuale degli elementi discendenti di un dato elemento può essere gestita con jQuery.
Possiamo creare il seguente plugin:
"use strict";
(function( $ ) {
$.fn.shuffle = function() {
$.each( this.get(), function( index, el ) {
var $el = $( el );
var $children = $el.children();
$children.sort(function() {
return 0.5 - Math.random();
});
$el.empty();
$children.appendTo( $el );
});
};
})(jQuery);
Il plugin opera sugli elementi discendenti dell'elemento selezionato e ne riorganizza l'ordine in modo casuale. Esempio:
$(function() {
$( "#shuffle" ).click(function() {
$( "#test" ).shuffle();
return false;
});
});