Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

jquery, html5, local storage

I have the jquery script below. and it is working. however, when I try to parse to html that have two same id name. It does not show the value any more. If I put one iabel name id. it works.

How can I make more than 1 label has value from the local storage codes below?


<script>
        window.onload = function() {
            retrieveData();
            retrieveSingleData();
        };
        function retrieveData() {
            for (var i = 0, len = localStorage.length; i < len; i++) {
                var key = localStorage.key(i);
                var value = localStorage[key];
                 
                if (value == "")
                {
                    value = "Missing";
                }
                $('#' + key).html(value);
                //alert(key + " => " + value);
            }
        }       
         
</script>

<label id="test1"></label>
<label id="test1"></label>

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

The rule is that 'id's must be unique.  JavaScript will only find the first one.  I don't know why you're getting nothing except that each 'id' should be different.
Avatar of ITsolutionWizard

ASKER

Because I need to show the values on different areas. What is my options to make it happen?
ASKER CERTIFIED SOLUTION
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your code is looping over the localStorage domain and finding key values which it is linking to elements in the document.

You only have one key : value pairing for each key value - so this should map to a single element. I don't understand why you have two lables with the same ID - do you want to set both to the same value?

If that is the case then rather use a class or a data-attribute and then use that in your selector for setting the values.

But I would be interested to know if you really want to target more than one element for a single key:value (localStorate) pair or if it should be a single LS value to a single document element?