Repository navigation
Expand file tree
/
Copy pathpayment.js
More file actions
132 lines (97 loc) · 2.95 KB
/
Copy pathpayment.js
File metadata and controls
132 lines (97 loc) · 2.95 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
displayNotes();
var addBtn = document.getElementById('addBtn');
// below event listener will add user input into the local storage
addBtn.addEventListener('click',function(){
let notesObj;
let addNote = document.getElementById('addNote');
let notesString = localStorage.getItem('notes');
if(notesString == null){
notesObj = [];
}
else{
notesObj = JSON.parse(notesString);
}
//Add date
let now = new Date();
let dateTime = `${now.getDate()}-${now.getMonth()+1}-${now.getFullYear()} | ${now.getHours()}:${now.getMinutes()}`;
//pushing into local storage
let tempObj = { text: addNote.value, time: dateTime };
notesObj.push(tempObj);
localStorage.setItem('notes',JSON.stringify(notesObj));
addNote.value = '';
displayNotes();
});
// funtion to display data stored in the local storage
function displayNotes(){
let notesObj;
let notesString = localStorage.getItem('notes');
if(notesString == null){
notesObj = [];
}
else{
notesObj = JSON.parse(notesString);
}
let html = '';
notesObj.forEach(function(element,index){
html += `
<div class="card mx-4 my-2 bg-dark text-white thatsMyNote" style="width: 18rem;">
<div class="card-body">
<h6>${element.time}</h6>
<p class="card-text">${element.text.replace(/</g, "<").replace(/>/g, ">")}</p>
<button id="${index}" onclick=deleteNote(this.id) class="btn btn-danger">Delete</button>
</div>
</div>
`;
});
let noteEle = document.getElementById('notes');
if(notesObj.length != 0){
noteEle.innerHTML = html;
}
else{
noteEle.innerHTML = '<h3 style="text-align: center; color: grey;">Nothing to display</h3>';
}
}
//function to delete a note
function deleteNote(index){
let notesObj;
let notesString = localStorage.getItem('notes');
if(notesString == null){
notesObj = [];
}
else{
notesObj = JSON.parse(notesString);
}
notesObj.splice(index,1);
localStorage.setItem('notes',JSON.stringify(notesObj));
displayNotes();
}
let search = document.getElementById('search');
search.addEventListener('input',function(e){
let inputText = search.value;
//below statement will be executed when the search bar is emptied using backspace
if(inputText == ''){
document.getElementById('noMatches').innerHTML = '';
}
var countNone = 0;
let cards = document.getElementsByClassName('thatsMyNote');
Array.from(cards).forEach(function(ele){
let cardText = ele.getElementsByTagName('p')[0].innerText;
if(cardText.includes(inputText)){
ele.style.display = 'block';
}
else{
ele.style.display = 'none';
countNone++;
if(countNone === cards.length){
document.getElementById('noMatches').innerHTML = '<h3 style="text-align: center; color: grey;">No matches found</h3>';
}
else{
document.getElementById('noMatches').innerHTML = '';
}
}
});
//Below code will be executed when the input text matches all the elements.
if(countNone === 0){
document.getElementById('noMatches').innerHTML = '';
}
});