-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMynotes.js
97 lines (88 loc) · 2.61 KB
/
Mynotes.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
87
88
89
90
91
92
93
94
95
96
97
console.log("Welcome To My Notes");
showNotes(); //To show notes at the beginning of the program
//1.To add notes
let addbtn = document.getElementById("addbtn");
addbtn.addEventListener("click", function (e) {
let addtitle = document.getElementById("addtitle");
let addtxt = document.getElementById("addtxt");
if (addtxt.value.length == 0 || addtitle.value.length == 0) {
alert("Title or Note Field is empty Please write something!");
} else {
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
//Object to store Title nd Note
let Myobj = {
title: addtitle.value,
text: addtxt.value,
};
notesObj.push(Myobj);
localStorage.setItem("notes", JSON.stringify(notesObj));
addtxt.value = "";
addtitle.value = "";
showNotes();
}
});
//2. function to show notes from local storage
function showNotes() {
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
let html = "";
notesObj.forEach(function (element, index) {
html += `
<div class="my-cards">
<h3 class="card-title">
${element.title}
</h3>
<p class="my-notes">
${element.text}
</p>
<button class="deletebtn" id="${index}" onclick="deleteNote(this.id)">DELETE NOTE</button>
</div>`;
});
let notesElm = document.getElementById("notes");
if (notesObj.length != 0) {
notesElm.innerHTML = html;
} else {
notesElm.innerHTML = `Nothing to show! use "Add notes" section above to add a note.`;
}
}
//3.function to delete a note
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();
}
//4.function to search a note
let search = document.getElementById("searchtxt");
let searchbtn = document.getElementById("clrbtn");
searchbtn.addEventListener("click", function () {
search.value = "";
});
search.addEventListener("input", function () {
let inputval = search.value.toLowerCase();
let notecards = document.getElementsByClassName("my-cards");
Array.from(notecards).forEach(function (element) {
let cardtxt = element.getElementsByTagName("p")[0].innerHTML;
console.log(cardtxt);
console.log(inputval);
if (cardtxt.includes(inputval)) {
element.style.display = "block";
} else {
element.style.display = "none";
}
});
});