0

I have the following code:

<script type="text/javascript">
        for (i=0;i<localStorage.length;i++) 
        {
            var key = localStorage.key(i);
            if(key == "5") 
            {
                var value = localStorage.getItem(key); 
                document.write(value + "<br />");
            }
        }
</script>

I have tried to print all the element with a specific content - for a demo it could be 5, but I couldn't get it.

Any help?

Or Amranov
  • 47
  • 1
  • 5
  • *sidenote:* in HTML5, `type` attribute in ` – Raptor May 13 '13 at 11:10
  • From where you Created LocalStorage, when Loading Page Itself there will not be any LocalStorage, – Akshay Joy May 13 '13 at 11:11
  • Are you sure that you have the key in local storage. You should console.log(localStorage), or check resourses tab in developer tools if you are using chrome. – Rok Burgar May 13 '13 at 11:18

2 Answers2

2

Given that LocalStorage (as all key-based storage systems) doesn't allow for multiple elements having the same key, there are some solutions:

  1. Store an array inside a single key and iterate over that array. Remember LocalStorage only supports strings, so you have to stringify/parse when writing/reading to LocalStorage:

    var elements = [], 
        elements[0] = {name: "Alpha"}, 
        elements[1] = {name: "Beta"}, 
        elements[2] = {name: "Gamma"};
    
    // Save
    localStorage.setItem(key,JSON.stringify(elements);
    // Read
    var elements = JSON.parse(localStorage.getItem(key));
    
  2. Use "dot notation" paths as keys and implement some sort of path parsing:

    var elements = [], 
        elements[0] = {name: "Alpha"}, 
        elements[1] = {name: "Beta"}, 
        elements[2] = {name: "Gamma"}, 
        elementsKey = 5;
    
    // Save
    for(var e = 0; e < elements.length; e ++) {
        // Elements get stored in "5.0", "5.1", "5.2", …
        var key = elementsKey + "." + e;
        localStorage.setItem(key,JSON.stringify(elements[e]);
    }
    
    // Read
    var baseKey = 5; // This is the "root" key to search for children
    for (var e = 0; e < localStorage.length; e++) {
        var key = localStorage.key(e);
        var subKey = key.split(".").pop(); // this is the "sub" key for this children element
        if(key.indexOf(baseKey) >= 0) {
            var element = localStorage.getItem(key);
        }
    }
    

Now i don't know what you are really storing inside those keys, but this should give you a good starting point.

sixFingers
  • 1,285
  • 8
  • 13
1

You need to create Local Storage Varibale Ebfore to Iterate, Your Iteration code will fire when Page Load happends

window.localStorage.setItem("1", "AKSAHY");
window.localStorage.setItem("2", "John");
window.localStorage.setItem("3", "Manu");
window.localStorage.setItem("4", "Albert");
window.localStorage.setItem("5", "Mathew");


 for (i=0;i<localStorage.length;i++) 
        {
            var key = localStorage.key(i);
            if(key == "5") 
            {
                var value = localStorage.getItem(key); 
                alert(value + "<br />");
            }
        }

Check DEMO :- DEMO

Akshay Joy
  • 1,765
  • 1
  • 14
  • 23