-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain-behaviour.js
More file actions
72 lines (56 loc) · 2.04 KB
/
main-behaviour.js
File metadata and controls
72 lines (56 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
window.onload = intit;
function intit() {
var button = document.getElementById("addButton");
button.onclick = createSticky;
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
if (key.substring(0, 6) == "sticky") {
var value = localStorage[key];
addStickyToDOM(key, value);
}
} // end for loop
} // end init function
function addStickyToDOM(key, value) {
var stickers = document.getElementById("stickers");
var sticky = document.createElement("li");
sticky.setAttribute("id", key);
sticky.setAttribute("title", "Click to delete");
var span = document.createElement("span");
span.setAttribute("class", "sticky");
span.innerHTML = value;
sticky.appendChild(span);
stickers.insertBefore(sticky, stickers.firstChild);
sticky.onclick = deleteSticky;
} // end addStickyToDOM function
function createSticky() {
var value = document.getElementById("notetxt").value;
if (document.getElementById("notetxt") && value) {
// Create date object and get current time in millisecond
var currentDate = new Date();
var time = currentDate.getTime();
// Set index or key for local storage according to current time
var key = "sticky_" + time;
try {
localStorage[key] = value;
} catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
alert("Out of Local Storage Capacity");
}
} // end try/catch
addStickyToDOM(key, value);
} else {
alert("Your Note is Empty");
}
} // end createSticky function
function deleteSticky(event) {
var key = event.target.id;
if (event.target.tagName.toLowerCase() == "span") {
key = event.target.parentNode.id;
} // end if
localStorage.removeItem(key);
removeStickyFromDOM(key);
}// end deleteSticky function
function removeStickyFromDOM(key) {
var sticky = document.getElementById(key);
sticky.parentNode.removeChild(sticky);
} // end removeStickyFromDOM function