Oladunni Faith Posted July 3, 2022 Share Posted July 3, 2022 Hello anyone? I'm trying to create a todo with my DOM knowledge of Javascript but I've been stuck because the forEach array creates element for each task more than once. Any help or suggestion on how to stop this would be much appreciated. Also I'm trying to implement saving selected state to local storage. any advice on how to go on this would be much appreciated. 'use-strict' window.addEventListener('load', () => { let localStorageKey = 'task.lists'; let tasks = JSON.parse(localStorage.getItem(localStorageKey)) || []; let selectedState = 'selected.list'; let selectedList = localStorage.getItem(selectedState); const form = document.querySelector('#head'); const input = document.querySelector('#text'); const container1 = document.querySelector('#item1'); const container2 = document.querySelector('#item2'); const container3 = document.querySelector('#item3'); form.addEventListener('submit', (e) => { e.preventDefault(); let todo = input.value; e.target.reset(); if (todo === null || '') { return alert('please Fill Out Task'); } else { let todoList = listItem(todo); tasks.push(todoList); console.log(todoList); console.log(todo); createAndSave(); tasks.forEach((task, i) => { let P1 = document.createElement('p'); container1.appendChild(P1); P1.innerText = task.name; let p2 = document.createElement('p'); container2.appendChild(p2); p2.innerText = 'Incomplete'; let actionContainer = document.createElement('div'); actionContainer.classList.add('button-container'); container3.appendChild(actionContainer); let button1 = document.createElement('button'); button1.setAttribute('id', 'button1'); actionContainer.appendChild(button1); let span1 = document.createElement('span'); span1.classList.add('material-symbols-outlined'); span1.innerText = 'done'; button1.appendChild(span1); let button2 = document.createElement('button'); button2.setAttribute('id', 'button2'); actionContainer.appendChild(button2); let span2 = document.createElement('span'); span2.classList.add('material-symbols-outlined'); span2.innerText = 'delete'; button2.appendChild(span2); button1.addEventListener('click', () => { P1.style.color = 'grey'; p2.style.color = 'green'; p2.innerText = 'COMPLETED' button1.style.color = 'white'; button1.style.backgroundColor = 'transparent'; }) button2.addEventListener('click', function () { deleteItem(); P1.remove(); p2.remove(); button1.remove(); button2.remove() }) }) return; } function createAndSave() { //createTask(); savePage(); } function savePage() { localStorage.setItem(localStorageKey, JSON.stringify(tasks)); localStorage.setItem(selectedState, JSON.stringify(selectedList)); } function deleteItem() { tasks.forEach(function (task, i) { tasks.splice(task[i], 1); localStorage.setItem(localStorageKey, JSON.stringify(tasks)) }) } function listItem(name) { return { id: Date.now().toString(), name: name, tasks: [] } } }) }) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now