HTML:
<div id="parent">
<div class="child">1</div>
<div class="child">2</div>
...
<div class="child">X</div>
</div>
I have shuffled the order of divs via JS. Now I want to restore original order.
I have tried this JS:
function restoreOrder() {
var container = document.getElementById("parent");
var elementsArray = Array.prototype.slice.call(container.getElementsByClassName('child'));
elementsArray.forEach(function(element){
container.removeChild(element);
})
restoreArray(elementsArray);
elementsArray.forEach(function(element){
container.appendChild(element);
})
}
var restoreArray = function(array) {
var m = array.length, t, i;
while (m) {
i = Math.floor(Math.random() * m--);
array[m] = i;
}
return $filter('parent')(array, 'child');
}
But it seems all elements of class child just disappear. Anyone can help with this?