forked from Ayushparikh-code/Web-dev-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
86 lines (64 loc) · 2.06 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
console.log("Welcome to Magic notes app. Write your notes here.");
showNotes();
let myBtn = document.getElementById('myBtn');
myBtn.addEventListener('click', function (e) {
let textArea = document.getElementById('textarea');
let notes = localStorage.getItem('notes');
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
notesObj.push(textArea.value);
localStorage.setItem("notes", JSON.stringify(notesObj));
textArea.value = " ";
showNotes();
})
function showNotes() {
let notes = localStorage.getItem('notes');
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes);
}
console.log(notesObj);
let html = "";
notesObj.forEach(function (element, index) {
html += `<div class="noteBox">
<h3 class="noteHeading">Note ${index + 1}</h3>
<p class="paraHeading">${element}</p>
<button class="buttonHeading" id="${index}" onclick="deleteNote(this.id)">Delete Note</button>
</div>`;
});
let notesElem = document.getElementById('notes');
if (notesObj.length !== 0) {
notesElem.innerHTML = html;
} else {
notesElem.innerHTML = `Nothing to show, create a new note from "Add a note" section above.`;
}
}
function deleteNote(index) {
let notes = localStorage.getItem('notes');
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
notesObj.splice(index, 1);
localStorage.setItem('notes', JSON.stringify(notesObj));
showNotes();
}
let search = document.getElementById('search');
search.addEventListener('input', function () {
let inputVal = search.value;
let noteBoxs = document.getElementsByClassName('noteBox');
Array.from(noteBoxs).forEach(function (element) {
let boxTxt = document.getElementsByTagName('p')[0].innerHTML;
if (boxTxt.includes(inputVal)) {
element.style.display = "block";
} else {
element.style.display = "none";
}
})
})