Web Analytics Made Easy -
StatCounter localStorage - CodingForum

Announcement

Collapse
No announcement yet.

localStorage

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • localStorage

    Guys help me with this code.The idea is to save new new inputs in an string and display them using html.Every time I add a new one the html dispalys it if I reload the page the items are still displayed and the first getItem method and if I reload again is still working but here is the problem.After I reload the page and I insert a new element in string then it wil display the just the lates inputs and will delete the ones from other sessions.
    If I insert now :"one","two","three" it I will display "one,two,three" if I reload it will stil display " one,two,three" which is good, but after the reload if I insert "four" it will display only "four" and I want to be displayed "one,two,three,four".How can I make this happen?

    <!DOCTYPE html>
    <html>
    <body>

    <div id="result"></div>
    <button onclick="reloadd()">Reload</button>
    <button onclick="clearF()">Clear</button>
    <input id="valoare">
    <button id="adauga" onclick="adauga()">+</button>
    <button onclick="nrElemente()">ElemNr?</button>

    <script>
    var cars=[];
    var two="kes";

    document.getElementById("result").innerHTML = localStorage.getItem("array1");






    function clearF(){
    window.localStorage.clear(); location.reload();

    }

    function adauga(){
    var x=document.getElementById('valoare').value;
    cars.push(x);

    localStorage.setItem("array1",cars);
    document.getElementById("result").innerHTML = localStorage.getItem("array1");


    }

    function reloadd(){
    location.reload();}

    function nrElemente(){
    alert(localStorage.length);}

    </script>

    </body>
    </html>

  • #2
    You set the items based on the array cars. After reload this array will be empty and subsequently will only contain the elements newly entered. You will have to rebuild this array based on the item read from local storage.
    If the array would keep it's entry during reload you would not need a storage.

    Comment

    Working...
    X