shuffle($("table")); function shuffle(tbl) { var arr = tbl.find("td"); for( var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x ); var tmp; var rows = tbl.find("tr").length var cols = tbl.find("tr:first td").length for (i = 0; i < rows; i++){ tmp = tbl.find("tr").eq(i); tmp.html() for (j = 0; j < cols; j++) tmp.append(arr[i*cols+j]); } }
This is definitely not the best code, this is just an approach. But it does work. In your case you need to use
shuffle($("table tbody"));
source share