|
| 1 | +console.log('Welcome to Magic Notes App!'); |
| 2 | + |
| 3 | +// Function Declaration Before Use |
| 4 | +function showNotes() { |
| 5 | + const notesObj = JSON.parse(localStorage.getItem('notes')) || []; |
| 6 | + const notesElem = document.getElementById('notes'); |
| 7 | + |
| 8 | + if (notesObj.length === 0) { |
| 9 | + notesElem.innerHTML = 'No notes added yet.'; |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + notesElem.innerHTML = notesObj |
| 14 | + .map( |
| 15 | + (note, index) => ` |
| 16 | + <div class="noteBox"> |
| 17 | + <h3 class="noteHeading">Note ${index + 1}</h3> |
| 18 | + <p class="paraHeading">${note}</p> |
| 19 | + <button class="buttonHeading" onclick="deleteNote(${index})">Delete Note</button> |
| 20 | + </div> |
| 21 | + `, |
| 22 | + ) |
| 23 | + .join(''); |
| 24 | +} |
| 25 | + |
| 26 | +// Event Listener for Add Note Button |
| 27 | +document.getElementById('myBtn').addEventListener('click', () => { |
| 28 | + const textArea = document.getElementById('textarea'); |
| 29 | + const noteContent = textArea.value.trim(); |
| 30 | + if (!noteContent) { |
| 31 | + alert('Please enter a note!'); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const notesObj = JSON.parse(localStorage.getItem('notes')) || []; |
| 36 | + notesObj.push(noteContent); |
| 37 | + localStorage.setItem('notes', JSON.stringify(notesObj)); |
| 38 | + textArea.value = ''; |
| 39 | + showNotes(); |
| 40 | +}); |
| 41 | + |
| 42 | +// Delete Note Function |
| 43 | +// eslint-disable-next-line no-unused-vars |
| 44 | +function deleteNote(index) { |
| 45 | + const notesObj = JSON.parse(localStorage.getItem('notes')) || []; |
| 46 | + notesObj.splice(index, 1); |
| 47 | + localStorage.setItem('notes', JSON.stringify(notesObj)); |
| 48 | + showNotes(); |
| 49 | +} |
| 50 | + |
| 51 | +// Search Notes |
| 52 | +document.getElementById('search').addEventListener('input', function () { |
| 53 | + const inputVal = this.value.toLowerCase().trim(); |
| 54 | + const noteBoxes = document.getElementsByClassName('noteBox'); |
| 55 | + |
| 56 | + Array.from(noteBoxes).forEach((element) => { |
| 57 | + const boxTxt = element |
| 58 | + .querySelector('.paraHeading') |
| 59 | + .innerText.toLowerCase(); |
| 60 | + element.style.display = boxTxt.includes(inputVal) ? 'block' : 'none'; |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +// Initial Call |
| 65 | +showNotes(); |
0 commit comments