-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
116 lines (108 loc) · 3.61 KB
/
main.js
File metadata and controls
116 lines (108 loc) · 3.61 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
// Initiating the Variables
const createButton = document.getElementById("createNoteButton");
const addNoteDiv = document.getElementById("addNoteDiv");
const noteBox = document.getElementById("noteBox");
const input = document.getElementById("input");
const textarea = document.getElementById("textarea");
const submit = document.getElementById("submit");
const search = document.getElementById("search");
// Event listener for search input matches to title and body of note
search.addEventListener("input", () => {
let val = search.value;
let allNotes = Array.from(document.getElementsByClassName("title-div"));
allNotes.forEach((element) => {
let result = element.getElementsByTagName("p")[0].innerText;
let result2 = element.getElementsByTagName("p")[1].innerText;
if (result.includes(val) || result2.includes(val)) {
element.style.display = "block";
} else {
element.style.display = "none";
}
});
});
// function to show notes added to local storage
const gridNote = () => {
let notesArray = localStorage.getItem("notes");
if (notesArray == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notesArray);
}
let template = "";
notesObj.forEach((note, index) => {
// template which will be populated in dom after adding new note
template += ` <div class="border-2 bg-white title-div border-blue-400 text-center p-4 rounded-md">
<p class="text-xl m-2 font-semibold break-words title ">${note.title}</p>
<p class="text-md text-left break-words body">${note.body}
</p>
<div class="m-2 mt-4 mb-0">
<button
id= ${index}
onClick="deleteNote(this.id)"
title="Delete Note"
class="p-2 focus:outline-none bg-red-600 hover:font-semibold text-sm border-2 border-red-600 text-white hover:bg-red-800 hover:border-2 font-bold hover:border-red-800 rounded-md m-2 ml-0"
>
<i class="bx bxs-trash-alt text-xl"></i>
</button>
</div>
</div>`;
});
let grid = document.getElementById("grid");
if (notesArray.length != 0) {
grid.innerHTML = template;
}
};
// calling function to show notes
gridNote();
// function to delete note available in localstorage
const deleteNote = (index) => {
let notesArray = localStorage.getItem("notes");
if (notesArray == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notesArray);
}
notesObj.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notesObj));
gridNote();
};
let textareaError;
let inputError;
createButton.addEventListener("click", () => {
noteBox.classList.remove("hidden");
addNoteDiv.classList.add("hidden");
});
// Validation for Input and textarea
const inputBlankChecker = (input) => {
if (input.value == "") {
document.getElementById("inputCheck").classList.remove("hidden");
inputError = true;
} else return (inputError = false);
};
const textareaBlankChecker = (text) => {
if (text.value == "") {
document.getElementById("textCheck").classList.remove("hidden");
textareaError = true;
} else textareaError = false;
};
// Recheck submitted data and add notes to local storage
submit.addEventListener("click", () => {
let notesArray = localStorage.getItem("notes");
inputBlankChecker(input);
textareaBlankChecker(textarea);
if (!textareaError && !inputError) {
let title = input.value;
let body = textarea.value;
let data = { title, body };
if (notesArray == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notesArray);
}
notesObj.push(data);
localStorage.setItem("notes", JSON.stringify(notesObj));
gridNote();
input.value = "";
textarea.value = "";
}
});