-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (71 loc) · 2.15 KB
/
index.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
//if user adds a note, add it to the local storage
const addBtn = document.getElementById('addBtn');
const notesContainer= document.getElementById('notes');
const search= document.getElementById('searchTxt');
showNotes();
addBtn.addEventListener('click', e=>{
const addTxt= document.getElementById('addTxt');
const notes= localStorage.getItem('notes');
if(notes==null){
notesObj= [];
}
else{
notesObj= JSON.parse(notes);
}
notesObj.push(addTxt.value.trim());
localStorage.setItem('notes', JSON.stringify(notesObj));
addTxt.value= "";
console.log(notesObj);
showNotes();
});
function showNotes(){
const notes= localStorage.getItem('notes');
if(notes==null){
notesObj= [];
}
else{
notesObj= JSON.parse(notes);
}
notesContainer.innerHTML= "";
notesObj.forEach((element, index)=>{
const html= `
<div class="note-card my-2 mx-2 card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Todo ${index+1}</h5>
<p class="card-text">${element}</p>
<a href="#" id="${index}" onclick= deleteNote(this.id) class="btn btn-primary delete-btn">Delete Note</a>
</div>
</div>
`;
notesContainer.innerHTML+= html;
});
};
//Function to delete a note
function deleteNote(index){
console.log('I am deleting Note no ', index);
const notes= localStorage.getItem('notes');
if(notes==null){
notesObj= [];
}
else{
notesObj= JSON.parse(notes);
}
notesObj.splice(index, 1);
localStorage.setItem('notes', JSON.stringify(notesObj));
showNotes();
console.log(notesObj);
}
//Searching
search.addEventListener('input', ()=>{
const inputVal= search.value;
let noteCard= document.getElementsByClassName('note-card');
Array.from(noteCard).forEach((element, index)=>{
let cardTxt= element.querySelector('p').textContent;
if(cardTxt.includes(inputVal)){
element.style.display= 'block';
}
else{
element.style.display= 'none';
}
});
});