Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
How do I display every element from my localStorage item?

i'm trying to create a simple To-do list, and my question is how do i get all elements of a single item in localStorage displayed?

pushing things into localStorage in a form of an array works fine, but only thing I see on my page is the first index of the "tasks" array.

const inputEl = document.getElementById("inputEl")
const submitBtn = document.getElementById("submit")
const clearBtn = document.getElementById("clearBtn")
const todoListContainer = document.getElementById("todoList")
const taskContainer = document.querySelector(".task")
const cancelBtn = document.querySelector(".cancelBtn")
const doneBtn = document.querySelector(".doneBtn")
const errorMsg = document.querySelector(".error")

let localStorageContent = localStorage.getItem("tasks")
let tasks = []

function createTask(){
    if(inputEl.value.length != 0){
        

    const newDiv = document.createElement("div")
    newDiv.classList.add("task")
    const newParagraph = document.createElement("p")
    const newCancelBtn = document.createElement("button")
    newCancelBtn.classList.add("cancelBtn")
    newCancelBtn.textContent = "X"
    const newDoneBtn = document.createElement("button")
    newDoneBtn.classList.add("doneBtn")
    newDoneBtn.textContent = "Done"

    todoListContainer.appendChild(newDiv)
    newDiv.appendChild(newParagraph)
    newDiv.appendChild(newCancelBtn)
    newDiv.appendChild(newDoneBtn)
    //^^ Creating a container for a new task, with all its elements and assigning the classes^^
    
    tasks.push(inputEl.value)
    localStorage.setItem("tasks", JSON.stringify(tasks))
    inputEl.value = ""

    newParagraph.textContent = JSON.parse(localStorageContent)

    errorMsg.textContent = ""

    }else{
        errorMsg.textContent = "You have to type something in!"
        errorMsg.classList.toggle("visibility")
        
    }

}

submitBtn.addEventListener("click", () =>{
    createTask()
    
    
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

When you execute JSON.parse(localStorageContent) you convert your string into an array, that's right.

But:

newParagraph.textContent = JSON.parse(localStorageContent)

is the same as:

newParagraph.textContent = JSON.parse(localStorageContent)[0]

So, you have to loop through your JSON.parse(localStorageContent) array... Therefore, create a new variable:

let tasksItem = JSON.parse(localStorageContent)

and loop on with .forEach method

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda