2014-03-11 12:50:57 +00:00
|
|
|
|
|
|
|
//like in my previous post, I will set the value of window.onload
|
|
|
|
|
|
|
|
//equals to init and then define it separately
|
|
|
|
|
|
|
|
window.onload = init;
|
|
|
|
|
|
|
|
function init(){
|
|
|
|
|
|
|
|
for (var i=0; i < localStorage.length; i++){
|
|
|
|
|
|
|
|
var key = localStorage.key(i);
|
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
if (key.substring(0, 6) === 'note'){ //will explain this
|
2014-03-11 12:50:57 +00:00
|
|
|
|
|
|
|
var value = localStorage.getItem(key);
|
|
|
|
|
|
|
|
addStickiesToPage(value); //we will create this one
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//this function will insert the sticky notes to the DOM
|
|
|
|
|
|
|
|
//inside the <ul> element we created in the index.html file
|
|
|
|
|
|
|
|
function addStickiesToPage(value){
|
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
var stickies = document.getElementById("note-");
|
2014-03-11 12:50:57 +00:00
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
var note = document.createElement("li");
|
2014-03-11 12:50:57 +00:00
|
|
|
|
|
|
|
var span = document.createElement("span");
|
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
span.setAttribute("class", "note");
|
2014-03-11 12:50:57 +00:00
|
|
|
|
|
|
|
span.innerHTML = value;
|
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
note.appendChild(span);
|
2014-03-11 12:50:57 +00:00
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
stickies.appendChild(note);
|
2014-03-11 12:50:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function init(){
|
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
var button = document.getElementById("add_note");
|
2014-03-11 12:50:57 +00:00
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
button.onclick = makenote; //we have to define this function
|
2014-03-11 12:50:57 +00:00
|
|
|
|
|
|
|
//continue the init function here as shown above by adding
|
|
|
|
|
|
|
|
//the for loop to get items from localStorage
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
function makenote(){
|
2014-03-11 12:50:57 +00:00
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
var value = document.getElementById("personalnote").value;
|
2014-03-11 12:50:57 +00:00
|
|
|
|
2014-04-14 12:30:30 +00:00
|
|
|
var key = "note_" + localStorage.length;
|
2014-03-11 12:50:57 +00:00
|
|
|
|
|
|
|
localStorage.setItem(key, value);
|
|
|
|
|
|
|
|
addStickiesToPage(value);
|
|
|
|
|
|
|
|
}
|