-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
108 lines (88 loc) · 2.51 KB
/
app.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
98
99
100
101
102
103
104
105
106
/* AARTI RATHI
Topic: Note Making App
My website - https://shinchancode.github.io/3d-react-portfolio/ */
console.log("Welcome to Note App");
showNotes();
// if user adds a note, add it to the local storage
let addBtn = document.getElementById('addbtn');
addBtn.addEventListener("click", function (e) {
let addTxt = document.getElementById("addtxt");
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes);
}
notesObj.push(addTxt.value);
localStorage.setItem("notes", JSON.stringify(notesObj));
addTxt.value = "";
console.log(notesObj);
showNotes();
});
// function to show elements from local Storage
function showNotes() {
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes);
}
let html = "";
notesObj.forEach(function (e, index) {
html += `<div class="notecard my-2 mx-2 card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Note ${index + 1}</h5>
<p class="card-text">${e}</p>
<button id="${index}" onclick="deleteNote(this.id)" class="btn btn-primary">Delete Note</button>
</div>
</div>`;
});
let notesElm = document.getElementById("notes");
if (notesObj.length != 0) {
notesElm.innerHTML = html;
}
else
{
notesElm.innerHTML=`Nothing to show ! \n Use Add notes Section :)`;
}
}
// Function to delete Node
function deleteNote(index)
{
console.log(`I m deleting ${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();
}
//function for search
let search=document.getElementById("searchTxt");
search.addEventListener("input",function (){
let inputVal=search.value.toLowercase;
let notecards=document.getElementsByClassName("notecard")
Array.from(notecards).forEach(function(e){
let cardText=e.getElementsByTagName("p")[0].innerText;
if(cardText.includes(inputVal))
{
e.style.display="block";
}
else
{
e.style.display="none";
}
})
});
/* Further features
1| Add title
2| Marking Node as important
3| Separate notes by user
4| Sink with server and host
*/