Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!