-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (42 loc) · 1.41 KB
/
app.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
50
51
52
53
54
55
const noteInput = document.getElementById('note'),
titleInput = document.getElementById('title'),
noteContainer = document.getElementById('note-container'),
submitBtn = document.getElementById('btn');
let notesArr = [],
noteCount = 0;
submitBtn.addEventListener("click", () => {
noteCount++;
if((noteInput.value === "") && (title.value === "")) {
alert("You must input something before you can submit");
return false;
} else if ((noteInput.value === "") || (titleInput.value === "")){
alert("Note cannot be empty or without a header")
} else {
const note = noteInput.value,
title = titleInput.value;
const new_note = {
title: title,
note: note
};
notesArr.push(new_note);
let noteDiv = document.createElement("div");
noteDiv.classList.add("note-div");
noteDiv.innerHTML = `
<p class="num">${noteCount}</p>
<h4 class="heading">${new_note.title}</h4>
<p class="text">${new_note.note}</p>
<button class="delete-btn">Delete</button>
`
noteContainer.appendChild(noteDiv);
noteInput.value = "";
titleInput.value = "";
}
});
noteContainer.addEventListener("click", (e) => {
if (e.target.className === "delete-btn") {
const lead = Array.from(noteContainer.children).indexOf(e.target.parentNode);
notesArr.splice(lead, 1);
localStorage.setItem("notes", JSON.stringify(notesArr));
e.target.parentNode.remove();
}
});