-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
62 lines (56 loc) · 1.64 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
const addTitle = document.getElementById('addTitle');
const addText = document.getElementById('addText');
const addNoteButton = document.getElementById('addNote');
const notesDiv = document.getElementById('notes');
showNotes();
function addNotes(){
let notes = localStorage.getItem('notes');
if(notes === null){
notes = [];
}else{
notes = JSON.parse(notes);
}
if(addText.value == ''){
alert('Add your note');
return;
}
const noteObj = {
title: addTitle.value,
text: addText.value,
}
addTitle.value = '';
addText.value = '';
notes.push(noteObj);
localStorage.setItem('notes', JSON.stringify(notes));
showNotes();
}
function showNotes(){
let notesHTML = '';
let notes = localStorage.getItem('notes');
if(notes === null){
return;
}else{
notes = JSON.parse(notes);
}
for(let i=0; i<notes.length; i++){
notesHTML += `<div class="note">
<button class="deleteNote" id=${i} onclick="deleteNote(${i})">Delete</button>
<span class="title"><strong style="font-size: 20px;">${notes[i].title === "" ? 'Note' : notes[i].title}</strong></span>
<div class="text">${notes[i].text}</div>
</div>
`
}
notesDiv.innerHTML = notesHTML;
}
function deleteNote(ind){
let notes = localStorage.getItem('notes');
if(notes === null){
return;
}else{
notes = JSON.parse(notes);
}
notes.splice(ind, 1);
localStorage.setItem('notes', JSON.stringify(notes));
showNotes();
}
addNoteButton.addEventListener('click', addNotes);