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(); }
todos, while not deleting anything. - Dmytrykslicemethod. javascript.ru/array/slice - Dmytrykslicedoes not remove anything from the array - Grundysplice. Thank you - Dmytryk