-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
103 lines (76 loc) · 2.57 KB
/
Copy pathscript.js
File metadata and controls
103 lines (76 loc) · 2.57 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
let notesCount = 0;
let notesID = 0;
function createNote(ID, text) {
notesCount++;
notesID++;
localStorage.setItem(String(ID), text);
const divOuter = document.createElement('div');
divOuter.className = 'noteOuter';
divOuter.id = ID;
const noteName = document.createElement('label');
noteName.className = 'noteName';
noteName.id = `note-${ID}`;
noteName.textContent = `Note №${ID}`;
const div = document.createElement('div');
div.className = 'note';
const note = document.createElement('textarea');
note.className = 'noteEdit';
note.value = text;
const button = document.createElement('button');
button.className = 'delete';
button.textContent = 'X';
button.id = `delete-${ID}`;
div.appendChild(note);
div.appendChild(button);
button.addEventListener('click', function() {
divOuter.remove();
localStorage.removeItem(divOuter.id);
notesCount--;
console.log(`Removed note №${divOuter.id}`);
});
note.addEventListener('input', function() {
localStorage.setItem(divOuter.id, note.value);
});
if (notesCount != 1){
const hr = document.createElement('hr');
hr.className = 'noteSeparator';
divOuter.appendChild(hr);
}
divOuter.appendChild(noteName);
divOuter.appendChild(div);
console.log(`Note №${ID}, text="${text}"`);
return divOuter;
}
function deleteNotes(){
console.log('START Deleting notes END');
while(noteHolder.firstChild) {
noteHolder.removeChild(noteHolder.firstChild);
}
localStorage.clear();
notesCount = 0;
notesID = 0;
}
function notesFromLocalStorage() {
console.log('START Notes from local storage')
for (let i = localStorage.length - 1; i >= 0; i--) {
const key = localStorage.key(i);
const value = localStorage.getItem(key);
notesID = Math.max(notesID, Number(key));
noteHolder.appendChild(createNote(key, value));
}
console.log('END Notes from local storage')
}
const input = document.getElementById('input');
const noteHolder = document.getElementById('noteHolder');
const createButton = document.getElementById('createNote');
const deleteAllButton = document.getElementById('delete-all');
createButton.addEventListener('click', function() {
if (input.value != ''){
noteHolder.appendChild(createNote(notesID, input.value));
input.value = '';
}
});
deleteAllButton.addEventListener('click', function() {
deleteNotes();
});
notesFromLocalStorage();