-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
50 lines (44 loc) · 1.28 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const form = document.querySelector("#form")
let table = document.querySelector("#students-table")
let studentsArr = JSON.parse(localStorage.getItem("students")) || [];
const addToUi = (std) => {
let tr = document.createElement('tr')
tr.innerHTML = `
<td>${std.id}</td>
<td>${std.name}</td>
<td>${std.age}</td>
<td>${std.email}</td>
<td>${std.phone}</td>
<td>
<button
id="${std.id}"
data-id="${std.id}"
onClick="deleteStudent(${std.id})"
>Delete</button>
</td>
`
table.appendChild(tr)
}
form.addEventListener("submit", (event) => {
event.preventDefault()
const studentData = {
id: "BTA00" + (studentsArr.length + 1),
name: form.name.value,
age: form.age.value,
email: form.email.value,
phone: form.tel.value
}
addToUi(studentData)
form.reset()
studentsArr.push(studentData)
localStorage.setItem("students", JSON.stringify(studentsArr))
})
document.addEventListener("DOMContentLoaded", () => {
studentsArr.forEach(student => {
addToUi(student)
});
})
const deleteStudent = (id) => {
let studentId = id.dataset.id;
document.getElementById(studentId).parentElement.parentElement.remove()
}