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

173
Views
Show the element saved in localStorage when reload the page

I'm learning js.

I want to show the saved li with localStorage created with appendChild when I reload the page.

<ul id="gfg">
  <li>Computer Network</li>
  <li>Data Structures</li>
</ul>

<button onclick="test()">Submit</button>
function test() {
    var node = document.createElement('li');
    node.textContent = 'Hi';
    abc = document.getElementById('gfg').appendChild(node);

    //localStorage
    localStorage.setItem('abc', 'abc');
    let a = localStorage.getItem('abc');
    console.log(abc);
}
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

We can use a pair of functions to work with your items in the storage:

function getStorageItems() {
    var data = localStorage.getItem('items');
    return data ? JSON.parse(data) : null;
}

function saveStorageItem(text) {
    var array = getStorageItems() || [];
    array.push(text);
    localStorage.setItem('items', JSON.stringify(array));
}

With this functions you can save and load an array of text (the text of each li). In the storage, we save a text, this functions also convert/parse your array to a JSON text.

We can create another function to add one li item to your page:

function addItem(text, save) {
    var node = document.createElement('li');
    node.textContent = text;
    document.getElementById('gfg').appendChild(node);

    if (save !== false)
        saveStorageItem(text);
}

By default, we save in the storage the text. Only if we pass save=false, we omit the save action. In this form, we can add and save an item and also only add (without saving).

Your test function, in this form, add and save the item:

function test() {
    addItem("Hi");
}

And a last function to load items from storage:

function loadFromStorage() {
    var array = getStorageItems();
    if (array) {
        for (var i = 0; i < array.length; i++)
            addItem(array[i], false);
    }
}

Here we use false in addItem because only want add the item, not saving again. You can invoke loadFromStorage to load your saved items.

NOTE: here I only save the text because is the only data required to create your HTML. If you need more data, I suggest you use an object and save/load this serialized object in the same way or the HTML.

about 4 years ago · Santiago Gelvez 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!