This code allows me to save all entered data in localStorage.

But when deleting a line and crossing it out, and then when updating the page, these actions are not saved and the full array of the entered data is displayed.

How can this be corrected? Thank you in advance for your response!

window.onload = function() { const addBtn = document.querySelector('.add'); const list = document.getElementById('list'); list.setAttribute('status', false); let todos = []; const setTodos = () => { localStorage.setItem('todos', JSON.stringify(todos)); }; document.body.addEventListener('click', (el) => { if(el.target.nodeName === "LI") { el.target.classList.toggle('checked'); el.target.setAttribute('status', true); console.log(el.target); setTodos(); } else if(el.target.className === "trash") { let parent = el.target.parentNode; setTodos(); parent.remove(); } }); const newLiToTodo = (todo) => { let li = document.createElement('li'); li.innerText = todo.name; const dellBtn = document.createElement('button'); const dellTxt = document.createTextNode('Dell'); dellBtn.className = 'trash'; dellBtn.appendChild(dellTxt); li.appendChild(dellBtn); list.insertBefore(li, list.childNodes[0]); // !!!!!!!!!!!!!!!!!! DELL LocalStprage //dellBtn.onclick = dell(dellBtn); }; // const dell = (el) => { // let removeEl = el.parentNode; // let removeElId = removeEl.id; // removeEl.remove(); // }; const newObj = (status) => { let todo = {}; let name = document.querySelector('.in').value; let date = document.querySelector('.date').value; let inputInfo = `${name} ${date}`; if(!name || !date) { alert('Введите необходимые данные'); return; } todo.name = inputInfo; todo.status = list.getAttribute('status'); todos.push(todo); newLiToTodo(todo); document.querySelector('.in').value = ''; document.querySelector('.date').value = ''; setTodos(); }; let getTodos = () => { if(localStorage.getItem('todos')) { todos = JSON.parse(localStorage.getItem('todos')); } todos.forEach(newLiToTodo); }; addBtn.onclick = () => { newObj(); }; getTodos(); } 
  • If I have not missed anything, then you just push in todos , while not deleting anything. - Dmytryk
  • And how can I delete? - AlexGorol
  • To remove an element from an array, you can use the slice method. javascript.ru/array/slice - Dmytryk
  • @ Dmytryk, slice does not remove anything from the array - Grundy
  • @Grundy, for sure. Wrong. splice . Thank you - Dmytryk

0