-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (87 loc) · 2.79 KB
/
index.js
File metadata and controls
95 lines (87 loc) · 2.79 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
let btnAdd = document.getElementById("btnAdd");
showNotes();
btnAdd.addEventListener("click", function () {
let txtArea = document.getElementById("txtArea");
const txt = txtArea.value;
let titleArea = document.getElementById("addTitle");
const title = titleArea.value;
if (txt && title) {
txtArea.value = "";
titleArea.value = "";
addToLocalStorage(txt, title);
}
});
// function to add item in local storage
function addToLocalStorage(txt, title) {
let notesObj = localStorage.getItem("notes");
if (notesObj) {
notesObj = JSON.parse(notesObj);
} else notesObj = [];
notesObj.push({ note: txt, title: title });
notesObj = JSON.stringify(notesObj);
localStorage.setItem("notes", notesObj);
showNotes();
}
// function to show notes
function showNotes() {
let target = document.getElementById("main-container");
let notesObj = localStorage.getItem("notes");
if (notesObj == null) {
notesObj = [];
} else notesObj = JSON.parse(notesObj);
let data = "";
for (let i = notesObj.length - 1; i >= 0; i--) {
data += `<div class="card my-3 mx-1" style="width: 18rem">
<div class="card-body">
<h5 class="card-title">${notesObj[i].title}</h5>
<hr/>
<p class="card-text mx-2">${notesObj[i].note}</p>
<a id = "${i}" onclick="deleteFromLocalStorage(this.id)" class="btn btn-danger">Delete</a>
</div>
</div>`;
}
if (notesObj.length === 0) {
data = "Add something to show...";
}
target.innerHTML = data;
}
// function to delete a single note using its id
function deleteFromLocalStorage(index) {
let notesObj = localStorage.getItem("notes");
notesObj = JSON.parse(notesObj);
notesObj.splice(index, 1);
notesObj = JSON.stringify(notesObj);
localStorage.setItem("notes", notesObj);
showNotes();
}
// add event listener to search button
let searchBtn = document.getElementById("searchBtn");
searchBtn.addEventListener("input", function (e) {
const value = e.target.value.toLowerCase();
if (value.length === 0) {
showNotes();
return;
}
let target = document.getElementById("main-container");
let notesObj = localStorage.getItem("notes");
if (notesObj == null) {
notesObj = [];
} else notesObj = JSON.parse(notesObj);
let data = "";
for (let i = notesObj.length - 1; i >= 0; i--) {
if (notesObj[i].note.includes(value)) {
data += `<div class="card my-3 mx-1" style="width: 18rem">
<div class="card-body">
<h5 class="card-title">${notesObj[i].title}</h5>
<hr/>
<p class="card-text mx-2">${notesObj[i].note}</p>
<a id = "${i}" onclick="deleteFromLocalStorage(this.id)" class="btn btn-danger">Delete</a>
</div>
</div>`;
}
}
if (data.length == 0) {
data = "No match found...";
}
target.innerHTML = data;
});