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

82
Views
localstorege every time update new one

hi can you please explain why every time i add an item to local storage it gets updated instead of adding a new item ,,i guess for my code id isn't unique ,,,, but i can't understand how to fix it ....

here is code

https://jsfiddle.net/adulik/8sqbcox2/28/

//add task list item
function addTaskListItem(id) {
    //get element by id
    const task = document.getElementById(id);
    let getItemFromStorage =  localStorage.getItem("data-item")
    if(!getItemFromStorage){
        getItemFromStorage = JSON.parse(getItemFromStorage)
        getItemFromStorage = [...getItemFromStorage,task.value]
        console.log("storage",getItemFromStorage)

    }else {
        localStorage.setItem("data-item", JSON.stringify([task.value]))

    }

}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think you twisted your condition. It should be if(getItemFromStorage)

//add task list item
function addTaskListItem(id) {
    //get element by id
    const task = document.getElementById(id);
    let getItemFromStorage =  localStorage.getItem("data-item")
    if(getItemFromStorage){ //modified the condition here from `!getItemFromStorage` to `getItemFromStorage`
        getItemFromStorage = JSON.parse(getItemFromStorage)
        getItemFromStorage = [...getItemFromStorage,task.value]
        console.log("storage",getItemFromStorage)
        localStorage.setItem("data-item", JSON.stringify(getItemFromStorage)) //update local storage with latest tasks
    }else {
        localStorage.setItem("data-item", JSON.stringify([task.value]))

    }

}

It's working fine after that modification https://jsfiddle.net/n7Lj3zdx/

about 4 years ago · Juan Pablo Isaza Report

0

Because you update the "data-item", not append new data into it.

localStorage.setItem("data-item", JSON.stringify([task.value]))

It should be something like this:

localStorage.setItem("data-item", JSON.stringify([...ItemFromStorage].append(newItem))
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!