Here's a basic Car Buy & Sell Website with JavaScript functionality. This example uses HTML, CSS, and JavaScript with local storage to persist data. It includes:
A form to add new cars
Display of all cars
Delete functionality
Simple styling
✅ Features:
Add cars with make, model, year, price, and description
View all cars
Delete cars
Data saved in browser's local storage
📁 File Structure
code Copy code
car-website/
├── index.html
├── style.css
└── script.js
📄 index.html
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Car Buy & Sell</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<h1>Car Buy & Sell</h1>
<form id="carForm">
<label>Make:</label>
<input type="text" id="make" required />
<label>Model:</label>
<input type="text" id="model" required />
<label>Year:</label>
<input type="number" id="year" required />
<label>Price ($):</label>
<input type="number" id="price" required />
<label>Description:</label>
<textarea id="description"></textarea>
<button type="submit">Add Car</button>
</form>
<div id="carList"></div>
</div>
<script src="script.js"></script>
</body>
</html>
🎨 style.css
css Copy code
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
margin: 0;
padding: 20px;
}
.container {
max-width: 800px;
margin: auto;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
form {
display: flex;
flex-direction: column;
gap: 10px;
margin-bottom: 20px;
}
input, textarea, button {
padding: 10px;
font-size: 16px;
}
#carList {
display: flex;
flex-direction: column;
gap: 15px;
}
.car-card {
background: #f9f9f9;
padding: 15px;
border-radius: 5px;
display: flex;
justify-content: space-between;
align-items: center;
}
.delete-btn {
background: #e74c3c;
color: white;
border: none;
padding: 5px 10px;
cursor: pointer;
}
🧠 script.js
javascript Copy code
// Load cars from local storage
let cars = JSON.parse(localStorage.getItem('cars')) || [];
// Save cars to local storage
function saveCars() {
localStorage.setItem('cars', JSON.stringify(cars));
}
// Render cars
function renderCars() {
const carList = document.getElementById('carList');
carList.innerHTML = '';
cars.forEach((car, index) => {
const carCard = document.createElement('div');
carCard.className = 'car-card';
carCard.innerHTML = `
<div>
<strong>${car.year} ${car.make} ${car.model}</strong><br>
$${car.price}<br>
${car.description || 'No description'}
</div>
<button class="delete-btn" onclick="deleteCar(${index})">Delete</button>
`;
carList.appendChild(carCard);
});
}
// Handle form submission
document.getElementById('carForm').addEventListener('submit', function (e) {
e.preventDefault();
const make = document.getElementById('make').value.trim();
const model = document.getElementById('model').value.trim();
const year = document.getElementById('year').value;
const price = document.getElementById('price').value;
const description = document.getElementById('description').value.trim();
if (!make || !model || !year || !price) {
alert('Please fill in all required fields.');
return;
}
const newCar = { make, model, year, price, description };
cars.push(newCar);
saveCars();
renderCars();
// Reset form
this.reset();
});
// Delete car
function deleteCar(index) {
if (confirm('Are you sure you want to delete this car?')) {
cars.splice(index, 1);
saveCars();
renderCars();
}
}
// Initial render
renderCars();
🚀 How to Use
Save the files in the same folder.
Open index.html in your browser.
Add cars via the form.
Delete cars using the "Delete" button.
🔒 Notes
Data is stored in local storage , so it will be lost if the browser is cleared.
For a production app, you’d need a backend (e.g., Node.js, Firebase, or a database).
You can expand this with features like:
Editing cars
Filtering/sorting
User authentication
Image uploads
Let me know if you want to add advanced features! 🚗✨