diff --git a/Note Taking Web App/js/app.js b/Note Taking Web App/js/app.js new file mode 100644 index 00000000..bc9bf598 --- /dev/null +++ b/Note Taking Web App/js/app.js @@ -0,0 +1,74 @@ +showNotes(); +let addBtn = document.getElementById("addBtn"); + +addBtn.addEventListener("click", function (e) { + let addTxt = document.getElementById("addTxt"); + let notes = localStorage.getItem("notes"); + if (notes == null) { + notesObj = []; + } else { + notesObj = JSON.parse(notes); + } + + notesObj.push(addTxt.value); + localStorage.setItem("notes", JSON.stringify(notesObj)); + addTxt.value = ""; + showNotes(); +}); + +function showNotes() { + let notes = localStorage.getItem("notes"); + if (notes == null) { + notesObj = []; + } else { + notesObj = JSON.parse(notes); + } + + let html = ""; + notesObj.forEach(function (element, index) { + html += ` +
+
+
Note ${index + 1}
+

${element}

+ +
+
+ `; + }); + + let notesElem = document.getElementById("notes"); + if (notesObj.length != 0) { + notesElem.innerHTML = html; + } else { + notesElem.innerHTML = `Nothing to show! `; + } +} + +function deleteNode(index) { + let notes = localStorage.getItem("notes"); + if (notes == null) { + notesObj = []; + } else { + notesObj = JSON.parse(notes); + } + + notesObj.splice(index, 1); + localStorage.setItem("notes", JSON.stringify(notesObj)); + showNotes(); +} + +let search = document.getElementById('searchTxt'); +search.addEventListener("input",function(){ + let inputVal = search.value; + let noteCards = document.getElementsByClassName('noteCard'); + Array.from(noteCards).forEach(function(element){ + let cardTxt = element.getElementsByTagName("p")[0].innerHTML; + if(cardTxt.includes(inputVal) ||cardTxt.includes(inputVal.toLowerCase() ) || cardTxt.includes(inputVal.toUpperCase())){ + element.style.display = "block"; + } + else{ + element.style.display ="none"; + } + }) +})