Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Javascript: Unable to pull from localStorage

Making a todo list for a class project - able to get the item to "add" and append to the list, but when you refresh the page, the item returns as undefined. Checking localStorage.todo, I see it pulling part of the values I need, but aren't in use yet, but not the actual innerText. I started adding bits for a line-through to check off completed items and a remove button, but that was commented out and hopefully isn't impacting things - was trying to isolate the error.

I'm guessing my issue is something in the localStorage.setItem, but I'm not sure what it would be. I've tried changing the newTodo.innerText value to other variables in use but nothing returns. Below returns an input value I enter, but again, is lost when the page refreshes.

const todoForm = document.querySelector('#todoForm');
const todoList = document.querySelector('#todoList');
let todoItem = document.querySelector('#todoItem');

// Pull from storage
const savedList = JSON.parse(localStorage.getItem('todo')) || [];
for (let i = 0; i < savedList.length; i++)
{
let newTodo = document.createElement('li');
newTodo.innerText = savedList[i].task;
todoList.appendChild(newTodo);

}

// Add Item
todoForm.addEventListener('submit', function(e){
    e.preventDefault();
    let newTodo = document.createElement('li');
    let newItem = document.querySelector('#todoItem').value;
    newTodo.innerText = newItem;
    todoForm.reset();
    todoList.appendChild(newTodo);

// Clear Form Field on submit and storage  

    savedList.push({ task: newTodo.innertext, isCompleted: false });
    localStorage.setItem('todo', JSON.stringify(savedList));

});
// Add Remove Button
    // let removeItem = document.createElement('button');
    // removeItem.innerText = "Remove";
    // newTodo.append(removeItem);

// Strike through item or remove item
// todoList.addEventListener('click', function(e){
//     if (e.target.tagName === 'BUTTON'){
//         e.target.parentElement.remove();
//     }
//     else if (e.target.tagName === 'LI'){
//         e.target.style.textDecoration = 'line-through';
//     }  
 
// });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

camelcase is wrong not innertext but innerText

savedList.push({ task: newTodo.innerText, isCompleted: false })
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda