Browser Storage CRUD Web App
Here's a complete web application that provides a CRUD (Create, Read, Update, Delete) interface for browser storage (using localStorage). This implementation includes HTML, CSS, and JavaScript in a single file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Browser Storage CRUD</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #2c3e50;
margin-bottom: 30px;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="number"],
textarea {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
textarea {
height: 100px;
resize: vertical;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
button:hover {
background-color: #2980b9;
}
.btn-danger {
background-color: #e74c3c;
}
.btn-danger:hover {
background-color: #c0392b;
}
.btn-success {
background-color: #2ecc71;
}
.btn-success:hover {
background-color: #27ae60;
}
.btn-secondary {
background-color: #95a5a6;
}
.btn-secondary:hover {
background-color: #7f8c8d;
}
.action-buttons {
display: flex;
gap: 10px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
tr:hover {
background-color: #f9f9f9;
}
.search-container {
margin-bottom: 20px;
}
.no-records {
text-align: center;
padding: 20px;
color: #7f8c8d;
}
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 500px;
border-radius: 8px;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
cursor: pointer;
}
.close:hover {
color: black;
}
</style>
</head>
<body>
<div class="container">
<h1>Browser Storage CRUD</h1>
<div class="form-group">
<label for="search">Search:</label>
<input type="text" id="search" placeholder="Search records...">
</div>
<button id="addNewBtn" class="btn-success">Add New Record</button>
<table id="recordsTable">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="recordsBody">
<!-- Records will be inserted here -->
</tbody>
</table>
<div id="noRecords" class="no-records">No records found. Add a new record to get started.</div>
</div>
<!-- Modal for Add/Edit -->
<div id="recordModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2 id="modalTitle">Add New Record</h2>
<form id="recordForm">
<input type="hidden" id="recordId">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" required>
</div>
<div class="form-group">
<label for="description">Description:</label>
<textarea id="description" required></textarea>
</div>
<div class="action-buttons">
<button type="submit" id="saveBtn" class="btn-success">Save</button>
<button type="button" id="cancelBtn" class="btn-secondary">Cancel</button>
</div>
</form>
</div>
</div>
<!-- Modal for Delete Confirmation -->
<div id="deleteModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2>Confirm Delete</h2>
<p>Are you sure you want to delete this record?</p>
<div class="action-buttons">
<button id="confirmDeleteBtn" class="btn-danger">Delete</button>
<button id="cancelDeleteBtn" class="btn-secondary">Cancel</button>
</div>
</div>
</div>
<script>
// DOM Elements
const recordsTable = document.getElementById('recordsTable');
const recordsBody = document.getElementById('recordsBody');
const noRecords = document.getElementById('noRecords');
const searchInput = document.getElementById('search');
const addNewBtn = document.getElementById('addNewBtn');
// Modal Elements
const recordModal = document.getElementById('recordModal');
const deleteModal = document.getElementById('deleteModal');
const modalTitle = document.getElementById('modalTitle');
const recordForm = document.getElementById('recordForm');
const recordIdInput = document.getElementById('recordId');
const nameInput = document.getElementById('name');
const descriptionInput = document.getElementById('description');
const saveBtn = document.getElementById('saveBtn');
const cancelBtn = document.getElementById('cancelBtn');
const confirmDeleteBtn = document.getElementById('confirmDeleteBtn');
const cancelDeleteBtn = document.getElementById('cancelDeleteBtn');
const closeButtons = document.querySelectorAll('.close');
// State
let records = [];
let recordToDelete = null;
// Initialize the app
function init() {
loadRecords();
setupEventListeners();
}
// Load records from localStorage
function loadRecords() {
const recordsJson = localStorage.getItem('crudRecords');
records = recordsJson ? JSON.parse(recordsJson) : [];
renderRecords();
}
// Save records to localStorage
function saveRecords() {
localStorage.setItem('crudRecords', JSON.stringify(records));
renderRecords();
}
// Render records to the table
function renderRecords(filter = '') {
recordsBody.innerHTML = '';
const filteredRecords = filter
? records.filter(record =>
record.name.toLowerCase().includes(filter.toLowerCase()) ||
record.description.toLowerCase().includes(filter.toLowerCase()))
: records;
if (filteredRecords.length === 0) {
recordsTable.style.display = 'none';
noRecords.style.display = 'block';
noRecords.textContent = filter ? 'No matching records found.' : 'No records found. Add a new record to get started.';
} else {
recordsTable.style.display = 'table';
noRecords.style.display = 'none';
filteredRecords.forEach(record => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${record.id}</td>
<td>${record.name}</td>
<td>${record.description}</td>
<td>
<button class="edit-btn" data-id="${record.id}">Edit</button>
<button class="delete-btn" data-id="${record.id}">Delete</button>
</td>
`;
recordsBody.appendChild(row);