forked from balendurawat/iNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
117 lines (88 loc) · 2.81 KB
/
notes.js
File metadata and controls
117 lines (88 loc) · 2.81 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
showNotes();
let addBtn = document.getElementById("addBtn");
addBtn.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 myObj = {
title : addTitle.value,
text: addTxt.value
}
notesObj.push(myObj);
localStorage.setItem("notes",JSON.stringify(notesObj));
addTxt.value = "";
showNotes();
});
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="card" style="inline-block;width:90%;">
<div class="container" style="display:flex; align-items:center;justify-content:center;flex-direction:column;border:2px solid black; margin:20px">
<h4 style="padding:10px;font-size:20px;font-wieght:bold;"><b>${element.title}</b></h4>
<p style="font-size:20px;font-weight:bold; margin:10px auto;">${element.text}</p>
<button id="${index}" onclick="deleteNote(this.id)" class="delbtn" style="border:2px solid red;background: red;color:white;font-size:18px;font-weight:bold; margin: 10px;cursor:pointer;">Delete Note</button>
</div>
</div>`;
});
let none=`
<p style="font-size:50px;font-weight:bold;">Nothing to show here yet ! Click on + to create one.</p>
`
let notesElm = document.getElementById("notes-container");
if(notesObj.length != 0){
notesElm.innerHTML = html;
}
else{
notesElm.innerHTML=none;
}
}
// lets delete the note of a particular index
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();
}
// searching a note
let search = document.getElementById('search');
search.addEventListener("input", function(){
let inputVal = search.value.toLowerCase();
// console.log('Input event fired!', inputVal);
let noteCards = document.getElementsByClassName('card');
Array.from(noteCards).forEach(function(element){
let cardTxt = element.getElementsByTagName("p")[0].innerText;
if(cardTxt.includes(inputVal)){
element.style.display = "inline-block";
}
else{
element.style.display = "none";
}
// console.log(cardTxt);
})
})