-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
137 lines (102 loc) · 3.56 KB
/
script.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// calling shownotes to show the notes (if any) from local storage
showNote();
// ========= EVENT LISTENER FOR ADD A NOTE ========
let addBtn = document.getElementById("add-btn");
addBtn.addEventListener("click", function (e) {
// acessing notes textarea and title inputs
let addNote = document.getElementById("note-content");
let addTitle = document.getElementById("note-title");
// avoiding empty notes to be added
if(addNote.value==""){
alert('Please write something to be added as notes!!!');
return;
}
// accessing Local Storage for checking if any added notes
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
}
else {
notesObj = JSON.parse(notes);
}
// object containing title and note values
let myObj = {
title: addTitle.value,
text: addNote.value
}
// push notes textarea and title values to notes object
notesObj.push(myObj);
// set the localStorage with notes array in "notes"
localStorage.setItem("notes", JSON.stringify(notesObj));
// empty the note textarea & title after adding notes to local Storage
addNote.value = "";
addTitle.value = "";
// displaying all added notes to screen
showNote();
});
// ======= FUNCTION TO SHOW ELEMENTS FROM LOCAL STORAGE ========
function showNote() {
// accessing notes from localStorage
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
// wrting html & css code using template literals
let html = "";
notesObj.forEach((element, index) => {
html += `<div class="note-box added-notes">
<h4 class="note-title">${element.title}</h4>
<p class="note-content">${element.text}</p>
<button id="${index}" onclick="deleteNote(this.id)" class="btn">Delete Note</button>
</div>`;
});
// displaying all notes by manipulating HTML code with id "notes"
let notesElem = document.getElementById('notes');
if(notesObj.length != 0){
notesElem.innerHTML = html;
}
else{ // if no notes are added yet
notesElem.innerHTML = `<p style="margin: 20px auto; font-size: 1.25rem;">Nothing to show!! Use "Add Note" to add your notes</p>`;
}
}
// ======== FUNCTION TO DELETE A NOTE ========
function deleteNote(index) {
// accessing notes from localStorage
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
// removing "index" note from notes array
notesObj.splice(index, 1);
// updating local storage after deletion
localStorage.setItem("notes", JSON.stringify(notesObj));
// calling show Notes after deleting note
showNote();
}
// ======== EVENT LISTENER FOR SEARCH A NOTE ========
let search = document.getElementById('search-txt');
search.addEventListener('input', function() {
let inputVal = search.value.toLowerCase();
// accessing all added notes elements
let allNotes = document.getElementsByClassName('added-notes');
Array.from(allNotes).forEach( element => {
// accessing the note content of each added note boxes
let noteContent = element.getElementsByTagName('p')[0].innerText.toLowerCase();
// if notecontent matches display otherwise not
if(noteContent.includes(inputVal)){
element.style.display = "block";
}else{
element.style.display = "none";
}
});
})
/* FURTHER FEATURES CAN BE ADDED:
1. Add Title
2. Mark a note as Impt
3. Separate notes by User (Custom Folder)
4. Sync and host to web server
*/