-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
117 lines (104 loc) · 3.21 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
107
108
109
110
111
112
113
114
115
116
117
console.log("hello");
//if user add the notes
let addbtn = document.getElementById('addbtn');
let clear = document.getElementById('clearAll');
//clear function
clear.addEventListener('click', function () {
//checks if the localStorage is empty or not
if (localStorage.length == 0) {
alert("Empty Localstorage!");
}
else {
localStorage.clear();
showNotes();
}
})
addbtn.addEventListener('click', function (e) {
let text = document.getElementById('addtext').value;
if (text == "") {
alert("Please add some text and title");
} else {
textNote = text;
let notes = localStorage.getItem('notes');
if (notes == null) {
notesobj = [];
}
else {
notesobj = JSON.parse(notes);
}
notesobj.push(textNote);
localStorage.setItem('notes', JSON.stringify(notesobj));
addtext.value = "";
console.log(notesobj);
showNotes();
}
});
//function to show all notes
function showNotes() {
let notes = localStorage.getItem('notes');
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes);
}
let html = "";
notesObj.forEach(function (note, index) {
html += `
<div class="noteCard mx-2 my-2 card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">${index + 1}. Note</h5>
<p class="card-text">${note}</p>
<button id= "${index}" onclick= "deletNote(this.id)" class="btn btn-primary">Delet Note</button>
</div>
</div>
`
});
let notesElem = document.getElementById('notes');
if (notesObj.length != 0) {
notesElem.innerHTML = html;
}
//If local storage is empty show this message
let emptyStorage = localStorage.getItem('notes');
if (emptyStorage == null) {
document.getElementById('notes').innerHTML = `You haven't created a note yet!`;
}
}
//If local storage is empty show this message
let emptyStorage = localStorage.getItem('notes');
if (emptyStorage == null) {
document.getElementById('notes').innerHTML = `You haven't created a note yet!`;
}
//function to delet the note
function deletNote(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();
if(notesObj == 0){
document.getElementById('notes').innerHTML = "You haven't created a note yet!"
}
}
// function for search elements
let searchText = document.getElementById('searchText');
searchText.addEventListener('input', function () {
let inputVal = searchText.value;
let noteCards = document.getElementsByClassName('noteCard');
Array.from(noteCards).forEach(function (element) {
let cardTxt = element.getElementsByTagName("p")[0].innerText;
if (cardTxt.includes(inputVal)) {
element.style.display = "block";
}
else {
element.style.display = "none";
}
})
})
const load=()=>{
showNotes();
}