-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
98 lines (82 loc) · 3.2 KB
/
app.js
File metadata and controls
98 lines (82 loc) · 3.2 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
console.log('This is MyNotes website');
showNotes();
let addBtn = document.getElementById('addBtn');
addBtn.addEventListener('click', function (e) {
let addTxt = document.getElementById('addTxt');
let addTitle = document.getElementById('addTitle');
let notes = localStorage.getItem('notes'); //We take note in local storage
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes);
}
let myObj = {
title: addTitle.value,
text: addTxt.value
}
notesObj.push(myObj); //push value of textarea in notesObj
localStorage.setItem('notes', JSON.stringify(notesObj)); //Set notes in local storage
addTxt.value = ""; //When we add some txt and click on add note textarea is blank
addTitle.value = "";
showNotes(); //showNotes function is call when we click on add note
})
function showNotes() {
let notes = localStorage.getItem('notes');
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes); //notes is contain in notesObj on form of object
}
let html = "";
notesObj.forEach(function (element, index) {
html += `<div class="noteCard my-2 mx-2 card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">${index + 1}. ${element.title}</h5>
<p class="card-text">${element.text}</p>
<button id="${index}" onclick="deleteNote(this.id)" class="btn">Delete Note</button>
</div>
</div>`;
//Here we set note
});
let notesEle = document.getElementById('notes');
if (notesObj.length != 0) {
notesEle.innerHTML = html;
}
else {
notesEle.innerHTML = `<h4 style="text-align: center;"> Nothing to show! Please Write a Note above.</h4>`;
}
}
//For Deleting Notes
function deleteNote(index) {
// console.log('Delete note', index);
let notes = localStorage.getItem('notes');
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes); //notes is contain in notesObj on form of object
}
notesObj.splice(index, 1);
localStorage.setItem('notes', JSON.stringify(notesObj)); //Update local storage
showNotes();
}
//Search notes
let search = document.getElementById('searchTxt');
search.addEventListener('input', function () {
let inputVal = search.value.toLowerCase();
// console.log('input event fired', inputVal);
let noteCards = document.getElementsByClassName('noteCard');
Array.from(noteCards).forEach(function (element, index) {
let cardTxt = document.getElementsByTagName("p")[index].innerText;
if (cardTxt.includes(inputVal)) {
// element.style.color = 'red';
element.style.display = "block";
}
else {
element.style.display = "none";
}
// console.log(cardTxt);
})
})