-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
129 lines (109 loc) · 4.16 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Array to store contacts as objects
let contacts = [];
// Load contacts from local storage on page load
document.addEventListener("DOMContentLoaded", () => {
loadContacts();
});
// Function to load contacts from local storage
function loadContacts() {
const storedContacts = localStorage.getItem("contacts");
if (storedContacts) {
// Parse the stored contacts into an array
contacts = JSON.parse(storedContacts);
// Display the contacts
displayContacts();
}
}
// Function to save contacts to local storage
function saveContacts() {
// Stringify and save contacts
localStorage.setItem("contacts", JSON.stringify(contacts));
}
// Function to show the relevant form based on the selected action
function showForm(action) {
const forms = document.querySelectorAll('.form-container');
forms.forEach(form => {
form.classList.add('hidden');
});
const selectedForm = document.getElementById(`${action}-form`);
// Show the selected form
selectedForm.classList.remove('hidden');
}
// Function to add a new contact
function addContact() {
const firstName = document.getElementById("firstName").value;
const lastName = document.getElementById("lastName").value;
const phoneNumber = document.getElementById("phoneNumber").value;
const address = document.getElementById("address").value;
// Create a new contact object
const newContact = { firstName, lastName, phoneNumber, address };
contacts.push(newContact);
contacts.sort((a, b) => (a.lastName + a.firstName).localeCompare(b.lastName + b.firstName));
// Save updated contacts to local storage
saveContacts();
displayContacts();
clearFields("firstName", "lastName", "phoneNumber", "address");
}
// Function to delete a contact
function deleteContact() {
const firstName = document.getElementById("deleteFirstName").value;
const lastName = document.getElementById("deleteLastName").value;
contacts = contacts.filter(contact => !(contact.firstName === firstName && contact.lastName === lastName));
// Save updated contacts to local storage
saveContacts();
displayContacts();
clearFields("deleteFirstName", "deleteLastName");
}
// Function to modify a contact's address
function modifyContact() {
const firstName = document.getElementById("modifyFirstName").value;
const lastName = document.getElementById("modifyLastName").value;
const newAddress = document.getElementById("newAddress").value;
const contact = contacts.find(contact => contact.firstName === firstName && contact.lastName === lastName);
if (contact) {
contact.address = newAddress;
alert("Address updated.");
// Save updated contacts to local storage
saveContacts();
} else {
alert("Contact not found.");
}
displayContacts();
clearFields("modifyFirstName", "modifyLastName", "newAddress");
}
// Function to search for a contact by name
function searchContact() {
const firstName = document.getElementById("searchFirstName").value;
const lastName = document.getElementById("searchLastName").value;
const contact = contacts.find(contact => contact.firstName === firstName && contact.lastName === lastName);
if (contact) {
alert(`Found Contact:\nName: ${contact.firstName} ${contact.lastName}\nPhone: ${contact.phoneNumber}\nAddress: ${contact.address}`);
} else {
alert("Contact not found.");
}
clearFields("searchFirstName", "searchLastName");
}
// Function to display all contacts
function displayContacts() {
const contactList = document.getElementById("contacts");
// Clear the previous contact list
contactList.innerHTML = '';
contacts.forEach(contact => {
const contactCard = document.createElement('div');
contactCard.className = 'contact-card';
contactCard.innerHTML = `
<p><strong>Name:</strong> ${contact.firstName} ${contact.lastName}</p>
<p><strong>Phone:</strong> ${contact.phoneNumber}</p>
<p><strong>Address:</strong> ${contact.address}</p>
`;
// Add each contact card to the contact list
contactList.appendChild(contactCard);
});
}
// Function to clear input fields
function clearFields(...fields) {
fields.forEach(field => {
// Clear the input field
document.getElementById(field).value = '';
});
}