So I have a list of children from a DOM element. I take the child list and push it to an empty array. When I appendChild the array elements one by one, why does it change the position of the children elements? Shouldn't it effect only the array?
var listItemsContainer = document.getElementById("$content_id");
var items = listItemsContainer.children;
var itemsArr = [];
var continentcontainer = document.getElementById('continents');
for (var i in items) { //for each item in listItemsContainer
itemsArr.push(items[i]); //add them to the itemsArray (not moving)
}
itemsArr.sort(function(a, b) { //sorting itemsArr (array of listItems) A to Z
return a.innerHTML == b.innerHTML ?
0 :
(a.innerHTML > b.innerHTML ? 1 : -1);
});
for (var i = 0; i < itemsArr.length; ++i) {
continentcontainer.appendChild(itemsArr[i]); //append child moves item from current location into new location
}