-
-
Notifications
You must be signed in to change notification settings - Fork 543
/
script.js
49 lines (43 loc) · 1.51 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
const addButton = document.getElementById("add");
const notes = JSON.parse(localStorage.getItem("notes"));
const updateLocalStorage = () => {
const notesText = document.querySelectorAll("textarea");
const notes = [];
notesText.forEach((note) => notes.push(note.value));
localStorage.setItem("notes", JSON.stringify(notes));
};
const addNewNote = (text = "") => {
const note = document.createElement("div");
note.classList.add("note");
note.innerHTML = `
<div class="tools">
<button class="edit"><i class="fas fa-edit"></i></button>
<button class="delete"><i class="fas fa-trash-alt"></i></button>
</div>
<div class="main ${text ? "" : "hidden"}"></div>
<textarea class="${text ? "hidden" : ""}"></textarea>`;
const editButton = note.querySelector(".edit");
const deleteButton = note.querySelector(".delete");
const main = note.querySelector(".main");
const textArea = note.querySelector("textarea");
textArea.value = text;
main.innerHTML = marked(text);
deleteButton.addEventListener("click", () => {
note.remove();
updateLocalStorage();
});
editButton.addEventListener("click", () => {
main.classList.toggle("hidden");
textArea.classList.toggle("hidden");
});
textArea.addEventListener("input", (e) => {
const { value } = e.target;
main.innerHTML = marked(value);
updateLocalStorage();
});
document.body.appendChild(note);
};
addButton.addEventListener("click", () => addNewNote());
if (notes) {
notes.forEach((note) => addNewNote(note));
}