-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
102 lines (91 loc) · 3.01 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const recordForm = document.getElementById('record-form');
const nameInput = document.getElementById('name');
const ageInput = document.getElementById('age');
const emailInput = document.getElementById('email');
const recordList = document.getElementById('record-list');
const editIndexInput = document.getElementById('edit-index');
// Initialize records from local storage
let records = JSON.parse(localStorage.getItem('records')) || [];
console.log(records.length);
// Function to check for duplicate names
function isDuplicateName(email) {
return records.some(
(record) => record.email.toLowerCase() === email.toLowerCase()
);
}
// Display records
function displayRecords() {
recordList.innerHTML = '';
console.log(records.length);
if (records.length === 0) {
const row = document.createElement('tr');
row.innerHTML = `<td colspan="5" style="text-align:center;color:red;">No Record Found</td>`;
recordList.appendChild(row);
} else {
records.forEach((record, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${record.name}</td>
<td>${record.age}</td>
<td>${record.email}</td>
<td><button onclick="editRecord(${index})">Edit</button></td>
<td class="deleteButton"><button onclick="deleteRecord(${index})">Delete</button></td>
`;
recordList.appendChild(row);
});
}
}
// Add or Update a record
recordForm.addEventListener('submit', function (e) {
e.preventDefault();
const name = nameInput.value;
const age = ageInput.value;
const email = emailInput.value;
const editIndex = parseInt(editIndexInput.value);
if (name && age && email) {
if (isDuplicateName(email) && editIndex === -1) {
alert('Student already exists.');
return;
}
if (editIndex === -1) {
// Add a new record
records.push({ name, age, email });
} else {
// Update an existing record
records[editIndex] = { name, age, email };
editIndexInput.value = -1;
}
localStorage.setItem('records', JSON.stringify(records));
nameInput.value = '';
ageInput.value = '';
emailInput.value = '';
displayRecords();
}
});
// Edit a record
function editRecord(index) {
const recordToEdit = records[index];
nameInput.value = recordToEdit.name;
ageInput.value = recordToEdit.age;
emailInput.value = recordToEdit.email;
editIndexInput.value = index;
}
// Delete a record
function deleteRecord(index) {
displayRecords();
let delBtn = document.querySelectorAll('.deleteButton');
console.log(delBtn);
delBtn[
index
].innerHTML = `<i id="yesBtn" onclick="confirmDelete(${index})" class="fa-solid fa-check"></i><i id="noBtn" onclick="resetDelete(${index})" class="fa-solid fa-xmark"></i>`;
}
function confirmDelete(index) {
records.splice(index, 1);
localStorage.setItem('records', JSON.stringify(records));
displayRecords();
}
function resetDelete(index) {
displayRecords();
}
// Initial display
displayRecords();