I have a three-column layout, and each column has multiple divs that hold blocks of similar content:
<section class="info-blocks-container clearfix">
<div id="col1">
<div class="info-block">...</div>
<div class="info-block">...</div>
<div class="info-block">...</div>
</div>
<div id="col2">
<div class="info-block">...</div>
<div class="info-block">...</div>
</div>
<div id="col3">
<div class="info-block">...</div>
<div class="info-block">...</div>
<div class="info-block">...</div>
</div>
</section>
I’d like to shuffle the children divs within div#col1, div#col2, and div#col3 amongst the columns randomly, so some of #col1‘s child divs end up in #col2 and/or #col3, and vice-versa. I’m using thecodeparadox’s function reorder() found here (I removed the ‘undo’ function) and the shuffling is working, but
1. the way I have 3 separate duplicate functions is very dirty, and
2. it’s only sorting the divs within each column (it’s not sharing the children divs among the three columns):
<script type="text/javascript">
$(function(){
reorder();
reorder2();
reorder3();
function reorder() {
var grp = $(".info-blocks-container > div:nth-child(1)").children();
var cnt = grp.length;
var temp, x;
for (var i = 0; i < cnt; i++) {
temp = grp[i];
x = Math.floor(Math.random() * cnt);
grp[i] = grp[x];
grp[x] = temp;
}
$(grp).remove();
$(".info-blocks-container > div:nth-child(1)").append($(grp));
}
function reorder2() {
var grp = $(".info-blocks-container > div:nth-child(2)").children();
var cnt = grp.length;
var temp, x;
for (var i = 0; i < cnt; i++) {
temp = grp[i];
x = Math.floor(Math.random() * cnt);
grp[i] = grp[x];
grp[x] = temp;
}
$(grp).remove();
$(".info-blocks-container > div:nth-child(2)").append($(grp));
}
function reorder3() {
var grp = $(".info-blocks-container > div:nth-child(3)").children();
var cnt = grp.length;
var temp, x;
for (var i = 0; i < cnt; i++) {
temp = grp[i];
x = Math.floor(Math.random() * cnt);
grp[i] = grp[x];
grp[x] = temp;
}
$(grp).remove();
$(".info-blocks-container > div:nth-child(3)").append($(grp));
}
});
</script>
Any suggestions?
At second, your problem was a really a challenge for me. But I think I found a solution:
It doesn’t looks professional, but it works. Hopefully it is what you requested for…