To create a simple JavaScript program for managing a list of names and phone numbers, we can utilize a combination of HTML and JavaScript. Since we're not dealing with a backend server or persistent database here, we'll simulate the database using local storage to preserve the data across browser sessions. Below is an example of how you can achieve this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Phonebook</title>
<style>
body {
font-family: Arial, sans-serif;
}
form {
margin-bottom: 20px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 10px;
border: 1px solid #ddd;
}
th {
background-color: #f4f4f4;
}
</style>
</head>
<body>
<h1>Phonebook</h1>
<form id="contactForm">
<input type="text" id="name" placeholder="Name" required>
<input type="text" id="phone" placeholder="Phone Number" required>
<button type="submit">Add Contact</button>
</form>
<table id="contactTable">
<thead>
<tr>
<th>Name</th>
<th>Phone Number</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- Contacts will be dynamically inserted here -->
</tbody>
</table>
<script>
const contactForm = document.getElementById('contactForm');
const contactTableBody = document.querySelector('#contactTable tbody');
let contacts = JSON.parse(localStorage.getItem('contacts')) || [];
function displayContacts() {
contactTableBody.innerHTML = ''; // Clear current table
contacts.forEach((contact, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${contact.name}</td>
<td>${contact.phone}</td>
<td>
<button onclick="deleteContact(${index})">Delete</button>
</td>
`;
contactTableBody.appendChild(row);
});
}
function deleteContact(index) {
contacts.splice(index, 1);
localStorage.setItem('contacts', JSON.stringify(contacts));
displayContacts();
}
contactForm.addEventListener('submit', (event) => {
event.preventDefault();
const name = document.getElementById('name').value;
const phone = document.getElementById('phone').value;
contacts.push({ name, phone });
localStorage.setItem('contacts', JSON.stringify(contacts));
displayContacts();
contactForm.reset(); // Clear the form
});
// Initial display
displayContacts();
</script>
</body>
</html>
Explanation:
-
HTML Structure: The HTML provides a form for entering new contacts and a table to display the stored contacts.
-
JavaScript Logic:
- Local Storage: We're using the browser's
localStorage to store contacts, so they persist even after refreshing the page.
- Display Contacts: Function
displayContacts updates the contact list in the table.
- Add Contacts: The form submission is handled by adding a new entry to the contacts array and saving it to local storage.
- Delete Contacts: Each contact row has a delete button that removes the contact from the array and updates local storage.
This provides a simple client-side "database" using localStorage for persistent storage of contact data between browser sessions. For a more robust solution, you would typically use a server-side database with an API.