0

I have multiple inputs on my page and store the data from them inside < span > ... < /span > by button click (since the text is typed and stored, it is considered not to disappear from those < span > fields).

        var newDate = document.getElementById('event_date');//my input
        var storageNewDate = newDate.value;//my text inside <input> which transfers it to <span>
        localStorage.setItem('Date', storageNewDate);//the data is stored

But I can't retrieve the stored data from localStorage. My Web Developer Tools show me that stored Data exists, but when I turn or reload the page I get my < span > fields empty.

    var insideSpanTags = document.getElementsByClassName("spanEvent");//all my <spans> 
      insideSpanTags.innerHTML = localStorage.getItem('Date');// I want my stored data to be shown there   

So, how can I get my stored data inside < span > tags every time I refresh or reload the page?

Alexandr Belov
  • 1,804
  • 9
  • 30
  • 43

1 Answers1

1

You need to loop your selectors

var insideSpanTags = document.getElementsByClassName("spanEvent");

for(var i=0; i<insideSpanTags.length; i++){
      insideSpanTags[i].innerHTML = localStorage.getItem('Date');
}

cause insideSpanTags is an Array collection of all your elements.


Since you're using an Array of objects, you need to JSON.stringify your Array of objects into localStorage. Than to retrieve the values use JSON.parse:

jsBin demo

var insideSpanTags = document.getElementsByClassName("spanEvent");
var events = [
    {
        'date': '7-1-2015',
        'event': 'Event Title',
        'participants': 'John, Robert',
        'description': 'Football'        
    },
    {
        'date': '23-1-2015',
        'event': 'Event Title 2',
        'participants': 'Peter, Rob',
        'description': 'Basketball'        
    },
];


// On Save, use this to store your events:
localStorage.evts = JSON.stringify( events );
// Now events are stored in LS


// After it's saved but also while first reading use:
var storedEvts = JSON.parse( localStorage.evts );
console.log( storedEvts );


// Just to test
storedEvts.forEach(function(el, idx){
  insideSpanTags[idx].innerHTML = el.date;
});

// Result:
//> 7-1-2015
//> 23-1-2015
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • That's it! Thanx! But can you tell me please, how can I get a particular data, because after I put the loop it shows me the same 'Date' data in each of tags. And I'd like to see a unique data inside each of tags. Like you create some events there (it's not what I created, it's just for explanation) http://jsfiddle.net/5d4vh891/ And then want it to be shown in its own cell (not the same event in each cell) – Alexandr Belov Feb 09 '15 at 20:24
  • @AlexandrBelov you need to learn how to store object inside a localStorage and than parse it back to retrieve the desired values. – Roko C. Buljan Feb 09 '15 at 20:26
  • @AlexandrBelov from your Question there's no way to know what kind of data you're storing. As I can see you're storing a single `ID`'s element `value`, so your comment is a bit odd. – Roko C. Buljan Feb 09 '15 at 20:27
  • Comment edited. Read it, please. – Alexandr Belov Feb 09 '15 at 20:30
  • 1
    @AlexandrBelov as I've said http://jsbin.com/vinugi/2/edit?html,js,console,output as you can see it's terribly easy. – Roko C. Buljan Feb 09 '15 at 21:21
  • @AlexandrBelov in Chrome, hit F12 and inspect LocalStorage under "Resources" tab. You'll see the localStorage.evts change while saving your data. – Roko C. Buljan Feb 09 '15 at 21:24