-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (37 loc) · 1.36 KB
/
app.js
File metadata and controls
49 lines (37 loc) · 1.36 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
const message = function () {
let notes = localStorage.getItem('notes');
notes === null ? notesObj = [] : notesObj = JSON.parse(notes);
}
let addBtn = document.getElementById('addBtn');
addBtn.addEventListener('click', function () {
let addText = document.getElementById('addText');
message();
notesObj.push(addText.value);
localStorage.setItem('notes', JSON.stringify(notesObj));
addText.value = '';
console.log(notesObj);
showNotes();
});
const showNotes = function () {
message();
let html = '';
notesObj.forEach(function (element, index) {
html +=
`<div class="noteCard my-2 mx-2 card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Note ${index}</h5>
<p class="card-text">${element}</p>
<button id="${index}" onclick="deleteNote(this.index)" class="btn btn-primary">Delete Notes</button>
</div>
</div>`
});
let notesElm = document.getElementById('notes');
notesObj.length != 0 ? notesElm.innerHTML = html :
notesElm.innerHTML = `Nothing to show! Use "Add a Note" section above to add Notes.`;
}
const deleteNote = function (index) {
message();
notesObj.splice(index, 1);
localStorage.setItem('notes', JSON.stringify(notesObj));
showNotes();
}