-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
97 lines (84 loc) · 2.52 KB
/
script.js
File metadata and controls
97 lines (84 loc) · 2.52 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
const addBtn = document.querySelector("#addBtn");
const main = document.querySelector("#main");
// Click event listener
addBtn.addEventListener("click", function () {
addNote();
});
// Save button function
const saveNotes = () => {
// Select content textareas
const notes =
document.querySelectorAll(".note .content");
// Select title textareas
const titles =
document.querySelectorAll(".note .title");
const data = [];
notes.forEach((note, index) => {
const content = note.value;
const title = titles[index].value;
console.log(title);
if (content.trim() !== "") {
data.push({ title, content });
}
});
const titlesData =
data.map((item) => item.title);
console.log(titlesData);
localStorage.setItem(
"titles", JSON.stringify(titlesData));
const contentData =
data.map((item) => item.content);
localStorage.setItem(
"notes", JSON.stringify(contentData));
};
// Addnote button function
const addNote = (text = "", title = "") => {
const note = document.createElement("div");
note.classList.add("note");
note.innerHTML = `
<div class="icons">
<i class="save fas fa-save"
style="color:red">
</i>
<i class="trash fas fa-trash"
style="color:yellow">
</i>
</div>
<div class="title-div">
<textarea class="title"
placeholder="Write the title ...">${title}
</textarea>
</div>
<textarea class="content"
placeholder="Note down your thoughts ...">${text}
</textarea>
`;
function handleTrashClick() {
note.remove();
saveNotes();
}
function handleSaveClick() {
saveNotes();
}
const delBtn = note.querySelector(".trash");
const saveButton = note.querySelector(".save");
const textareas = note.querySelectorAll("textarea");
delBtn.addEventListener("click", handleTrashClick);
saveButton.addEventListener("click", handleSaveClick);
main.appendChild(note);
saveNotes();
};
// Loading all the notes those are saved in
// the localstorage
function loadNotes() {
const titlesData =
JSON.parse(localStorage.getItem("titles")) || [];
const contentData =
JSON.parse(localStorage.getItem("notes")) || [];
for (let i = 0;
i < Math.max(
titlesData.length, contentData.length); i++) {
addNote(contentData[i], titlesData[i]);
}
}
loadNotes();