|
| 1 | +function fng(){ |
| 2 | + localStorage.clear() |
| 3 | + document.getElementById("main").innerHTML="" |
| 4 | + location.reload() |
| 5 | +} |
| 6 | +var num=0; |
| 7 | +var boy=1; |
| 8 | +var girl=1; |
| 9 | +var student; |
| 10 | +window.onload = ()=>{ |
| 11 | + |
| 12 | + |
| 13 | +var data=localStorage.getItem("all") |
| 14 | +var data1=localStorage.getItem("pres") |
| 15 | +if(data || data1){ |
| 16 | + var students=JSON.parse(data) |
| 17 | + var l=students.length |
| 18 | +document.getElementById("studnetnum").innerHTML=l |
| 19 | +document.getElementById("studnetnum1").innerHTML=l |
| 20 | + |
| 21 | + |
| 22 | +for(let i=0;i<students.length;i++){ |
| 23 | + |
| 24 | + studata(students[i]) |
| 25 | + |
| 26 | + if(students[i].genderof === "male"){ |
| 27 | + document.getElementById("boy").innerHTML=boy++ |
| 28 | + } |
| 29 | + else if(students[i].genderof === "female"){ |
| 30 | + document.getElementById("girl").innerHTML=girl++ |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +document.getElementById("add").addEventListener("click",()=>{ |
| 38 | + location.reload() |
| 39 | + |
| 40 | + const name = document.getElementById("name").value; |
| 41 | + const email1 = document.getElementById("email1").value; |
| 42 | + const gender = document.getElementById("gender").value; |
| 43 | +var all= |
| 44 | + { nameof: name , |
| 45 | + email1of: email1 , |
| 46 | + genderof: gender } |
| 47 | + |
| 48 | + student=JSON.parse( localStorage.getItem("all"))||[] |
| 49 | +student.push(all) |
| 50 | + |
| 51 | +localStorage.setItem("all",JSON.stringify(student)) |
| 52 | +studata(all) |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +} |
| 59 | +) |
| 60 | + |
| 61 | +function studata(student){ |
| 62 | + |
| 63 | + |
| 64 | +var main=document.getElementById("main") |
| 65 | +var div=document.createElement("Div") |
| 66 | + |
| 67 | +div.innerHTML+=` |
| 68 | +<div class="col-12 d-flex flex-column"> |
| 69 | +
|
| 70 | + <div class="d-flex col-12 flex-row justify-content-around align-content-end "> |
| 71 | +<div class="d-flex col-10 px-1 mt-3"> |
| 72 | +
|
| 73 | +<p class="px-2 col-4"> ${student.nameof}<p> |
| 74 | + |
| 75 | +<p class="px-2 col-6">${student.email1of}</p> |
| 76 | +<p class="px-2 col-2">${student.genderof}</p> |
| 77 | +</div> |
| 78 | +<div class="d-flex col-2 flex-row justify-content-end align-content-end"> |
| 79 | +<button class="edit"><i class="far fa-edit"></i></button> |
| 80 | +<button class="remove-btn"><i class="fa-solid fa-trash-can"></i></button> |
| 81 | +</div> |
| 82 | +</div> |
| 83 | +</div> |
| 84 | +` |
| 85 | +div.querySelector('.edit').addEventListener('click', () => { |
| 86 | +document.getElementById("editModal").style.display="block" |
| 87 | +// console.log(editGender.value); |
| 88 | +document.getElementById("saveEdit").addEventListener("click",()=>{ |
| 89 | + var editName=document.getElementById("editName").value |
| 90 | + var editEmail=document.getElementById("editEmail").value |
| 91 | + var editGender=document.getElementById("gender1").value |
| 92 | + // console.log(student.nameof="none"); |
| 93 | + div.innerHTML=` |
| 94 | + <div class="col-12 d-flex flex-column"> |
| 95 | +
|
| 96 | + <div class="d-flex col-12 flex-row justify-content-around align-content-end "> |
| 97 | +<div class="d-flex col-10 px-1 mt-3"> |
| 98 | +
|
| 99 | +<p class="px-2 col-4"> ${student.nameof=editName}<p> |
| 100 | + |
| 101 | +<p class="px-2 col-6">${student.email1of=editEmail}</p> |
| 102 | +<p class="px-2 col-2">${student.genderof=editGender}</p> |
| 103 | +</div> |
| 104 | +<div class="d-flex col-2 flex-row justify-content-end align-content-end"> |
| 105 | +<button class="edit"><i class="far fa-edit"></i></button> |
| 106 | +<button class="remove-btn" id="re" ><i class="fa-solid fa-trash-can"></i></button> |
| 107 | +</div> |
| 108 | +</div> |
| 109 | +</div> |
| 110 | + ` |
| 111 | + document.getElementById("re").addEventListener("click",()=>{ |
| 112 | + div.remove() |
| 113 | + }) |
| 114 | + document.getElementById("editModal").style.display="none" |
| 115 | +}) |
| 116 | +}) |
| 117 | + |
| 118 | + |
| 119 | +div.querySelector('.remove-btn').addEventListener('click', () => { |
| 120 | + div.remove(); |
| 121 | + |
| 122 | + let students = JSON.parse(localStorage.getItem("all")); |
| 123 | +localStorage.setItem("all", JSON.stringify(students)); |
| 124 | + let index = -1; |
| 125 | + for (let i = 0; i < students.length; i++) { |
| 126 | + if (students[i].nameof === student.nameof && students[i].email1of === student.email1of) { |
| 127 | + index = i; |
| 128 | + |
| 129 | + // break; |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + if (index !== -1) { |
| 137 | + students.splice(index, 1); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + localStorage.setItem("all", JSON.stringify(students)); |
| 142 | + |
| 143 | + |
| 144 | + let l = students.length; |
| 145 | + document.getElementById("studnetnum").innerHTML = l; |
| 146 | + document.getElementById("studnetnum1").innerHTML = l; |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +}); |
| 152 | + |
| 153 | +main.appendChild(div); |
| 154 | + |
| 155 | + |
| 156 | +} |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +// let num = 0; |
| 163 | +// let boy = 0; |
| 164 | +// let girl = 0; |
| 165 | +// // Load data from localStorage on page load |
| 166 | +// window.onload = function() { |
| 167 | +// var savedData = localStorage.getItem("students"); |
| 168 | +// if (savedData) { |
| 169 | +// var students = JSON.parse(savedData); |
| 170 | +// for (var i = 0; i < students.length; i++) { |
| 171 | +// addStudentToDOM(students[i]); |
| 172 | +// } |
| 173 | +// } |
| 174 | +// }; |
| 175 | +// // localStorage.clear() |
| 176 | +// document.getElementById("add").addEventListener("click", () => { |
| 177 | +// const name = document.getElementById("name").value; |
| 178 | +// const email1 = document.getElementById("email1").value; |
| 179 | +// const gender = document.getElementById("gender").value; |
| 180 | + |
| 181 | +// const student = { |
| 182 | +// nameof: name, |
| 183 | +// email1of: email1, |
| 184 | +// genderof: gender |
| 185 | +// }; |
| 186 | + |
| 187 | +// const students = JSON.parse(localStorage.getItem("students")) || []; |
| 188 | +// // console.log(students); |
| 189 | +// students.push(student); |
| 190 | +// localStorage.setItem("students", JSON.stringify(students)); |
| 191 | + |
| 192 | +// addStudentToDOM(student); |
| 193 | +// // updateCounts(students); |
| 194 | +// }); |
| 195 | +// // localStorage.clear() |
| 196 | +// function addStudentToDOM(student) { |
| 197 | +// console.log(student); |
| 198 | +// const main = document.getElementById("main"); |
| 199 | +// const div = document.createElement("div"); |
| 200 | + |
| 201 | +// div.innerHTML = ` |
| 202 | +// <div class="col-12 d-flex flex-column student-entry"> |
| 203 | +// <div class="d-flex col-12 flex-row justify-content-around align-content-end"> |
| 204 | +// <div class="d-flex col-10 px-1 mt-3"> |
| 205 | +// <p class="px-2 col-4">${student.nameof}</p> |
| 206 | +// <p class="px-2 col-5">${student.email1of}</p> |
| 207 | +// <p class="px-2 col-4">${student.genderof}</p> |
| 208 | +// </div> |
| 209 | +// <div class="d-flex col-2 flex-row justify-content-end align-content-end"> |
| 210 | +// <button class="edit-btn">Edit</button> |
| 211 | +// <button class="remove-btn">Remove</button> |
| 212 | +// </div> |
| 213 | +// </div> |
| 214 | +// </div> |
| 215 | +// `; |
| 216 | +// main.appendChild(div); |
| 217 | + |
| 218 | +// div.querySelector('.edit-btn').addEventListener('click', () => editStudent(student, div)); |
| 219 | + // div.querySelector('.remove-btn').addEventListener('click', () => removeStudent(student, div)); |
| 220 | +// } |
| 221 | + |
0 commit comments