-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
94 lines (68 loc) · 2.51 KB
/
script.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
if ('serviceWorker' in navigator) {
// register service worker
navigator.serviceWorker.register('service-worker.js');
}
let count = Number(window.localStorage.getItem("count"));
if(!count){
window.localStorage.setItem("count", "0");
}
function createNote(noteTitle, noteBody){
document.getElementById("no-notes").classList.add("hidden");
let li = document.createElement("li");
let a = document.createElement("a");
let h2 = document.createElement("h2");
let p = document.createElement("p");
let xButton = document.createElement("button");
xButton.classList.add("delete");
let xText = document.createTextNode("X");
let h2TN = document.createTextNode(noteTitle);
let pTN = document.createTextNode(noteBody);
h2.appendChild(h2TN);
p.appendChild(pTN);
xButton.appendChild(xText);
a.appendChild(h2);
a.appendChild(xButton);
a.appendChild(p);
a.setAttribute("href", "#");
li.appendChild(a);
document.getElementById("notes").appendChild(li);
}
function createNoteFromInput(e){
e.preventDefault();
let noteTitle = document.getElementById("new-note-title-input").value;
let noteBody = document.getElementById("new-note-body-input").value;
document.getElementById("new-note-title-input").value = "";
document.getElementById("new-note-body-input").value = "";
count+=1;
window.localStorage.setItem("count", count);
while(window.localStorage.getItem(noteTitle)){
noteTitle += "-1";
}
window.localStorage.setItem(noteTitle, noteBody);
createNote(noteTitle, noteBody);
}
function removeItem(e){
if(e.target.classList.contains("delete")){
if(confirm("Are you sure you want to delete the note?")){
let li = e.target.parentElement.parentElement;
let ul = document.getElementById("notes");
ul.removeChild(li);
}
}
count -=1;
window.localStorage.setItem("count", count);
window.localStorage.removeItem(e.target.previousElementSibling.innerText);
if(count<1){
document.getElementById("no-notes").className = "";
}
}
for (i = 0; i< count; i++){
let noteTitle = window.localStorage.key(i);
let noteBody = window.localStorage.getItem(noteTitle);
if(noteTitle !=="count" && noteTitle){
createNote(noteTitle, noteBody);
}
}
document.getElementById("inputForm")
.addEventListener("submit", createNoteFromInput, false);
document.getElementById("notes").addEventListener("click", removeItem);