|
1 |
| -const notesContainerEl = document.getElementById("app"); |
2 | 1 | const btnEl = document.getElementById("btn");
|
| 2 | +const appEl = document.getElementById("app"); |
3 | 3 |
|
4 | 4 | getNotes().forEach((note) => {
|
5 | 5 | const noteEl = createNoteEl(note.id, note.content);
|
6 |
| - notesContainerEl.insertBefore(noteEl, btnEl); |
| 6 | + appEl.insertBefore(noteEl, btnEl); |
7 | 7 | });
|
8 | 8 |
|
9 |
| -btnEl.addEventListener("click", () => addNote()); |
10 |
| - |
11 |
| -function getNotes() { |
12 |
| - return JSON.parse(localStorage.getItem("note-ap") || "[]"); |
13 |
| -} |
14 |
| - |
15 |
| -function saveNotes(notes) { |
16 |
| - localStorage.setItem("note-ap", JSON.stringify(notes)); |
17 |
| -} |
18 |
| - |
19 | 9 | function createNoteEl(id, content) {
|
20 | 10 | const element = document.createElement("textarea");
|
21 |
| - |
22 | 11 | element.classList.add("note");
|
23 |
| - element.value = content; |
24 | 12 | element.placeholder = "Empty Note";
|
25 |
| - |
26 |
| - element.addEventListener("input", () => { |
27 |
| - updateNote(id, element.value); |
28 |
| - }); |
| 13 | + element.value = content; |
29 | 14 |
|
30 | 15 | element.addEventListener("dblclick", () => {
|
31 |
| - const noteDelete = confirm("Want to Delete the note?"); |
32 |
| - if (noteDelete) { |
| 16 | + const warning = confirm("Do you want to delete this note?"); |
| 17 | + if (warning) { |
33 | 18 | deleteNote(id, element);
|
34 | 19 | }
|
35 | 20 | });
|
36 | 21 |
|
| 22 | + element.addEventListener("input", () => { |
| 23 | + updateNote(id, element.value); |
| 24 | + }); |
| 25 | + |
37 | 26 | return element;
|
38 | 27 | }
|
39 | 28 |
|
| 29 | +function deleteNote(id, element) { |
| 30 | + const notes = getNotes().filter((note)=>note.id != id) |
| 31 | + saveNote(notes) |
| 32 | + appEl.removeChild(element) |
| 33 | +} |
| 34 | + |
| 35 | +function updateNote(id, content) { |
| 36 | + const notes = getNotes(); |
| 37 | + const target = notes.filter((note) => note.id == id)[0]; |
| 38 | + target.content = content; |
| 39 | + saveNote(notes); |
| 40 | +} |
| 41 | + |
40 | 42 | function addNote() {
|
41 | 43 | const notes = getNotes();
|
42 | 44 | const noteObj = {
|
43 | 45 | id: Math.floor(Math.random() * 100000),
|
44 | 46 | content: "",
|
45 | 47 | };
|
46 |
| - |
47 | 48 | const noteEl = createNoteEl(noteObj.id, noteObj.content);
|
48 |
| - notesContainerEl.insertBefore(noteEl, btnEl); |
| 49 | + appEl.insertBefore(noteEl, btnEl); |
49 | 50 |
|
50 | 51 | notes.push(noteObj);
|
51 |
| - saveNotes(notes); |
52 |
| -} |
53 |
| - |
54 |
| -function updateNote(id, newContent) { |
55 |
| - const notes = getNotes(); |
56 |
| - const target = notes.filter((note) => note.id == id)[0]; |
57 | 52 |
|
58 |
| - target.content = newContent; |
59 |
| - saveNotes(notes); |
| 53 | + saveNote(notes); |
60 | 54 | }
|
61 | 55 |
|
62 |
| -function deleteNote(id, element) { |
63 |
| - const notes = getNotes().filter((note) => note.id != id); |
| 56 | +function saveNote(notes) { |
| 57 | + localStorage.setItem("note-app", JSON.stringify(notes)); |
| 58 | +} |
64 | 59 |
|
65 |
| - saveNotes(notes); |
66 |
| - notesContainerEl.removeChild(element); |
| 60 | +function getNotes() { |
| 61 | + return JSON.parse(localStorage.getItem("note-app") || "[]"); |
67 | 62 | }
|
| 63 | + |
| 64 | +btnEl.addEventListener("click", addNote); |
0 commit comments