I have a section with few divs inside:
<section class="list">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</section>
I prepended an extra item in the section with jQuery:
<section class="list">
<div class="extra-item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</section>
What I want is to randomly sort this extra prepended item within the section – so all the other items are preserving its order but this extra one is sorted on every page refresh:
i.e 1:
<section class="list">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="extra-item"></div>
</section>
i.e 2:
<section class="list">
<div class="item"></div>
<div class="item"></div>
<div class="extra-item"></div>
<div class="item"></div>
</section>
etc...
With the code below I managed to randomly sort all the items inside the section; but would it be possible to simply re-order this prepended item and keep the rest in its order?
$(function () {
var parent = $(".list");
var divs = parent.children();
while (divs.length) {
parent.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);
}
});
Thanks