-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (88 loc) · 2.74 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
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
showNotes();
let addBtn = document.getElementById("addBtn");
addBtn.addEventListener("click", addNotes);
function addNotes(){
//select title box and note box
let titleBox = document.getElementById("title");
let noteBox = document.getElementById("note");
//if it is empty then show alert massage
if(titleBox.value=="" || noteBox.value==""){
let alert = document.getElementById("alert");
alert.innerHTML+= `<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Carefull!! </strong>Please first add Your notes!!!
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
}else{
let notesObj = {
title : titleBox.value,
note : noteBox.value
}
//check localstorage notes parameter
let notes = localStorage.getItem("notes"); //we get json
// console.log(notes)
if(notes==null){
arr = []; //if not make empty array
}else{
arr = JSON.parse(notes) //parse json and convert to js array
}
arr.push(notesObj) //object push karre into array
localStorage.setItem("notes",JSON.stringify(arr)); //set this array into localstorage as value
//empty title and note box
titleBox.value ="";
noteBox.value ="";
showNotes();
}
}
function showNotes(){
let notes = localStorage.getItem("notes");//string type
if(notes == null){
arr = [];
}else{
arr = JSON.parse(notes); //object
}
// console.log(arr.length)
let html = "";
arr.forEach(function(ele,index){
html+=`<div class="col noteCard">
<div class="card">
<div class="card-body">
<h5 class="card-title">${ele.title}</h5>
<p class="card-text">${ele.note}</p>
<a href="#" id="${index}" class="btn btn-primary" onclick="delNotes(this.id)">Delete</a>
</div>
</div>
</div>`;
})
let cards = document.getElementById("notes");
if(arr.length!=0){
cards.innerHTML=html;
}else{
cards.innerHTML= `<h3 class="text-danger">There is no notes ....!!!</h3>`;
}
}
function delNotes(index){
let notes = localStorage.getItem("notes");
if(notes==null){
arr=[];
}else{
arr = JSON.parse(notes);
}
arr.splice(index,1);
localStorage.setItem("notes",JSON.stringify(arr))
showNotes();
}
let searchBox = document.getElementById("search");
searchBox.oninput = function(){
let searchText = searchBox.value.toLowerCase();
let allCards = document.getElementsByClassName("noteCard"); //array returns of same class
Array.from(allCards).forEach(function(ele){
// console.log(ele)
let allHeads = ele.getElementsByTagName("h5")[0].innerHTML;
if(allHeads.includes(searchText)){
ele.style.display = "block";
}else{
ele.style.display = "none";
}
console.log(allHeads)
})
}