-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.js
77 lines (74 loc) · 2.01 KB
/
note.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
console.log("hii");
shownote();
//when save button is clicked note is saved.
let savebtn= document.getElementById('savebtn');
savebtn.addEventListener("click", function(e){
let addtitle =document.getElementById('addtitle');
let addtxt = document.getElementById('addtxt');
let notes=localStorage.getItem('notes');
if(notes == null){
notesobj=[];
}else{
notesobj=JSON.parse(notes);
}
let addobj={
title: addtitle.value,
text: addtxt.value
}
notesobj.push(addobj);
localStorage.setItem("notes",JSON.stringify(notesobj));
addtxt.value="";
addtitle.value="";
console.log(notesobj);
shownote();
});
//Saved note is displayed
function shownote(){
let notes=localStorage.getItem('notes');
if(notes == null){
notesobj=[];
}else{
notesobj=JSON.parse(notes);
}
let html="";
let yournotes=document.getElementById('urnotes');
notesobj.forEach((element,index) => {
html=html+`
<div class="card mx-2 my-2" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">${element.title}</h5>
<p class="card-text">${element.text}</p>
<button onclick="deleteNote(this.id)" class="btn btn-primary" id="${index}">Delete Node</button>
</div>
</div>
`
});
if(notesobj.length != 0){
yournotes.innerHTML=html;
}else{
yournotes.innerHTML=`Nothing to display`;
}
}
//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));
shownote();
}
//Search function
function search(){
console.log("search is called");
let notes= localStorage.getItem("notes");
let searchinput=document.getElementById('searchin');
if(notes.includes(searchinput.value)){
card.style.display = "block";
} else{
card.style.display = "none";
}
}