-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
49 lines (43 loc) · 1.3 KB
/
main.js
File metadata and controls
49 lines (43 loc) · 1.3 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
const notesContainer = document.querySelector(".notes-container");
const createBtn = document.querySelector(".btn");
let notes = document.querySelectorAll(".input-box");
function showNotes() {
notesContainer.innerHTML = localStorage.getItem("notes");
}
showNotes();
function updateStorage() {
localStorage.setItem("notes", notesContainer.innerHTML);
}
createBtn.addEventListener("click", () => {
let inputBox = document.createElement("p");
let img = document.createElement("img");
inputBox.className = "input-box";
inputBox.setAttribute("contenteditable", "true");
img.src = "./images/delete.png";
inputBox.appendChild(img);
notesContainer.appendChild(inputBox);
});
notesContainer.addEventListener("click", (e) => {
if (e.target.tagName === "IMG") {
e.target.parentElement.remove();
updateStorage();
} else if (e.target.tagsName === "p") {
notes = document.querySelectorAll(".input-box");
notes.forEach((note) => {
note.onkeyup = function () {
updateStorage();
};
});
}
});
notesContainer.addEventListener("keyup", (e) => {
if (e.target.classList.contains("input-box")) {
updateStorage();
}
});
document.addEventListener("keydown", () => {
if (event.key === "Enter") {
document.execCommand("insertLineBreak");
event.preventDefault();
}
});