-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (56 loc) · 1.95 KB
/
Copy pathscript.js
File metadata and controls
69 lines (56 loc) · 1.95 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
const addBtn = document.querySelector("#add_note");
const noteCont = document.querySelector(".notes");
addBtn.addEventListener("click", addNote);
const notes = JSON.parse(localStorage.getItem("notes"));
if (notes){
notes.forEach((noteText) => {
addNote(noteText);
});
};
function addNote(text = ""){
if (typeof text !== "string"){
text = "";
}
const noteEl = document.createElement("div");
noteEl.classList.add("note_div");
noteEl.innerHTML = `
<div class="header">
<p class="editing ${text ? "hidden" : ""}">Editing...</p>
<div class="edit_delete">
<button type="button" class="edit_note">
<i class="fa-solid fa-pen update_task"></i>
</button>
<button type="button" class="delete_note">
<i class="fa-solid fa-trash-can delete_tasks"></i>
</button>
</div>
</div>
<div class="note ${text ? "" : "hidden"}">${text}</div>
<textarea class="write ${text ? "hidden" : ""}">${text}</textarea>
`;
const note = noteEl.querySelector(".note");
const input = noteEl.querySelector(".write");
const edit = noteEl.querySelector(".edit_note");
const del = noteEl.querySelector(".delete_note");
const header = noteEl.querySelector(".editing");
edit.addEventListener("click", () => {
note.classList.toggle("hidden");
input.classList.toggle("hidden");
header.classList.toggle("hidden");
});
del.addEventListener("click", () => {
noteEl.remove();
updateStorage();
});
input.addEventListener("input", (e) => {
note.innerHTML = e.target.value;
updateStorage();
});
noteCont.appendChild(noteEl);
};
function updateStorage(){
const notesText = document.querySelectorAll("textarea");
const notes = [];
notesText.forEach((note) => notes.push(note.value));
localStorage.setItem("notes", JSON.stringify(notes));
}