The reason it's restoring the previous entries is because your method to remove doesn't update the parent
myFavoriteFoodsArray
, it only updates a local array called
food
.
Then in the
form
submit
handler you append the new food item onto the parent
myFavoriteFoodsArray
and save that back to the local storage, which still has the removed food in it.
A very simple fix for this would be to overwrite the parent array once you've finished removing the food you want:
var food = [];
$('.listItem .listContentText').each(function() {
food.push($(this).text());
localStorage.setItem("myFavoriteFoods", JSON.stringify(food));
});
myFavoriteFoodsArray = food;