|
| 1 | +console.log("Welcome to Notes App. This is app.js"); |
| 2 | + |
| 3 | +showNotes(); |
| 4 | + |
| 5 | +let addBtn = document.getElementById("addBtn"); |
| 6 | +addBtn.addEventListener("click", function (e) { |
| 7 | + let addTxt = document.getElementById("addTxt"); |
| 8 | + let notes = localStorage.getItem("notes"); |
| 9 | + if (notes == null) { |
| 10 | + notesObj = []; |
| 11 | + } else { |
| 12 | + notesObj = JSON.parse(notes); |
| 13 | + } |
| 14 | + notesObj.push(addTxt.value); |
| 15 | + localStorage.setItem("notes", JSON.stringify(notesObj)); |
| 16 | + addTxt.value = ""; |
| 17 | + showNotes(); |
| 18 | +}); |
| 19 | + |
| 20 | +function showNotes() { |
| 21 | + let notes = localStorage.getItem("notes"); |
| 22 | + if (notes == null) { |
| 23 | + notesObj = []; |
| 24 | + } else { |
| 25 | + notesObj = JSON.parse(notes); |
| 26 | + } |
| 27 | + let html = ""; |
| 28 | + |
| 29 | + notesObj.forEach((element, index) => { |
| 30 | + html += `<div class="card notecard my-2 mx-2" style="width: 18rem"> |
| 31 | + <div class="card-body"> |
| 32 | + <h5 class="card-title">Note ${index + 1}</h5> |
| 33 | + <p class="card-text">${element}</p> |
| 34 | + <button class="btn btn-primary" onclick="deleteNote(${index})">Delete Note</button> |
| 35 | + </div> |
| 36 | + </div>`; |
| 37 | + }); |
| 38 | + let notesElem = document.getElementById("notes"); |
| 39 | + if (notesObj.length == 0) { |
| 40 | + notesElem.innerHTML = `<p>Nothing to show here. Try to add tasks using 'Add Notes' button!</p>`; |
| 41 | + } else { |
| 42 | + notesElem.innerHTML = html; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function deleteNote(index) { |
| 47 | + console.log("Delete called on " + index); |
| 48 | + let notes = localStorage.getItem("notes"); |
| 49 | + if (notes == null) { |
| 50 | + notesObj = []; |
| 51 | + } else { |
| 52 | + notesObj = JSON.parse(notes); |
| 53 | + } |
| 54 | + notesObj.splice(index, 1); |
| 55 | + localStorage.setItem("notes", JSON.stringify(notesObj)); |
| 56 | + showNotes(); |
| 57 | +} |
| 58 | + |
| 59 | +let searchTxt = document.getElementById("searchtxt"); |
| 60 | +searchTxt.addEventListener("input", function (e) { |
| 61 | + // console.log(searchNote.value); |
| 62 | + let noteCards = document.getElementsByClassName("notecard"); |
| 63 | + Array.from(noteCards).forEach(function (element) { |
| 64 | + let cardTxt = element.getElementsByTagName("p")[0]; |
| 65 | + if ( |
| 66 | + cardTxt.innerText.toLowerCase().includes(searchTxt.value.toLowerCase()) |
| 67 | + ) { |
| 68 | + element.style.display = "block"; |
| 69 | + } else { |
| 70 | + element.style.display = "none"; |
| 71 | + } |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +/** |
| 76 | + * Further features: |
| 77 | + * 1. Add Title |
| 78 | + * 2. Mark a note as Important |
| 79 | + * 3. Separate notes by user |
| 80 | + * 4. Sync and host to web server |
| 81 | + */ |
0 commit comments