Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to make a to-do list project and storing the tasks locally. I can save data locally to the browser but when I am trying to remove a task and then adding a new task then the previously removed tasks will be re-added again.

What I have tried:

JavaScript
const form = document.querySelector('form');
const input = document.getElementById('item');
let myFavoriteFoodsArray = localStorage.getItem('myFavoriteFoods') ? JSON.parse(localStorage.getItem('myFavoriteFoods')) : [];
localStorage.setItem('myFavoriteFoods', JSON.stringify(myFavoriteFoodsArray));

form.addEventListener('submit', function (e) {
  e.preventDefault();
  var item = $.trim($('#item').val());
  myFavoriteFoodsArray.push(input.value);
  localStorage.setItem('myFavoriteFoods', JSON.stringify(myFavoriteFoodsArray));
  input.value = "";
  $("body").append('<div class="listItem">'+ item +'</div>');
});

// Call each items from localStorage and append
JSON.parse(localStorage.getItem("myFavoriteFoods")).forEach(item => {
  $("body").append('<div class="listItem">^__i class="fas fa-trash">'+ item +'</div>');
});

// Remove single task from localStorage
var removeFood = function() {
  // Checking for last item and if it reached last div then remove localStorage and reload the page
  var numItems = $('.listItem').length;
  console.log(numItems);
  if (numItems > 1) {
    $(this).parent('.listItem').remove();
  } if (numItems == 1 || numItems == 0) {
    localStorage.removeItem("myFavoriteFoods");
    $(this).parent('.listItem').remove();
    location.reload();
  }

  /*var temp = [];
  const list = $( '.listItem' ).find( ".listContentText" ).get();
  $(".listItem").each(function(){
    var txt = $(this).find(".listContentText").text();
    temp.push(txt);
  });*/

  //declare an array
  var food = [];
  //get all instances of the element and iterate through each one
  $('.listItem .listContentText').each(function() {
      food.push($(this).text()); // Update the cart
      localStorage.setItem("myFavoriteFoods", JSON.stringify(food)); // store cart
      //console.log(localStorage.getItem("items"))
  });
};

// Call function for removing a single entry
$(document).on('click', '.fa-trash', removeFood);
// Clear myFavoriteFoods localStorage
$(document).on('click', '#taskClear', function () {
    localStorage.removeItem("myFavoriteFoods");
    location.reload();
});
Posted
Updated 16-May-21 22:46pm
v2

1 solution

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:

JavaScript
var food = [];
//get all instances of the element and iterate through each one
$('.listItem .listContentText').each(function() {
    food.push($(this).text()); // Update the cart
    localStorage.setItem("myFavoriteFoods", JSON.stringify(food)); // store cart
    //console.log(localStorage.getItem("items"))
});
myFavoriteFoodsArray = food; // <-- Added this here
 
Share this answer
 
Comments
Galarist_00 17-May-21 6:48am    
Wow! Thanks a lot! It was so simple that I have never thought about it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900