-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
61 lines (52 loc) · 1.45 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
const button = document.querySelector('button');
const container = document.querySelector('.container');
const addNote = (text = '') => {
const main = document.createElement('div');
main.classList.add('main');
main.innerHTML = `<div class="tool">
<i class="save fa-sharp fa-solid fa-floppy-disk"></i>
<i class="remove fa-sharp fa-solid fa-trash"></i>
</div>
<textarea>${text}</textarea>`;
container.appendChild(main);
main.querySelector('.remove').addEventListener('click', () => {
main.remove();
saveNote();
});
main.querySelector('.save').addEventListener('click', () => {
saveNote()
});
main.querySelector('textarea').addEventListener('focusout',()=>{
saveNote();
});
}
const saveNote = () => {
const notes = document.querySelectorAll('.main textarea');
let data = [];
notes.forEach((note) => {
data.push(note.value);
});
if(notes.length == 0){
localStorage.removeItem('notes');
location.replace('index.html');
}
else{
localStorage.setItem('notes', JSON.stringify(data));
}
}
(
function () {
let lsnotes = JSON.parse(localStorage.getItem('notes'));
if (lsnotes == null) {
addNote();
}
else {
lsnotes.forEach((lnote) => {
addNote(lnote);
});
}
}
)()
button.addEventListener('click', () => {
addNote();
});