Home > front end >  data won't retrieve from local storage
data won't retrieve from local storage

Time:09-07

i have created a simple todo list site and i wanted to store data in local storage with 0 experience in JavaScript but some how i managed to store the data under key (todos) and now im stuck on how to retrieve data i searched a lot of articles and tried all solutions but non worked like getitem and document.write here is my code please help

let todoItems = [];


function addTodo(text) {
    const todo = {
        text,
        checked: false,
        id: Date.now()
    };
    todoItems.push(todo);
    console.log(todoItems);
    renderTodo(todo);
    
}

            <div >
                <div class = "todos-list__li-text-1"> ${todo.text}</div>
                <svg  width="170" height="4" viewBox="0 0 194 4" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path id="line" d="M0 2L194 2" stroke="url(#paint0_linear)" stroke-width="3.2"/>
                        <defs>
                    <linearGradient id="paint0_linear" x1="194" y1="2.00012" x2="0" y2="2.00003" gradientUnits="userSpaceOnUse">
                    <stop stop-color="#7DCEE8"/>
                    <stop offset="1" stop-color="#30BDEA"/>
                    </linearGradient>
                    </defs>
                </svg>
            </div> 
            
            <button ><i ></i>  </button>
            </label>
        `;
    console.log(node);
    list.append(node);
    window.localStorage.setItem("todos", JSON.stringify(todoItems));
}

where exactly should i put the retrieve code ?

photo of the local storage

CodePudding user response:

You should be able to retrieve your list back with: localStorage.getItem('todos')

CodePudding user response:

try using this window.localStorage.getItem("todos");

i would suggest that you check in the first place under(in google chrome dev tool) application -> localstorage if your value is stored or not

CodePudding user response:

you retrieve items by localStorage.getItem, but localStorage only store strings, so in order to get the iterable of your store item, you can:

const items = JSON.parse(localStorage.getItem("todos"));
  • Related