-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwishlist.js
More file actions
37 lines (32 loc) · 1.36 KB
/
wishlist.js
File metadata and controls
37 lines (32 loc) · 1.36 KB
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
document.addEventListener('DOMContentLoaded', () => {
const wishlistContainer = document.getElementById('wishlist-container');
let wishlist = JSON.parse(localStorage.getItem('wishlist')) || [];
function renderWishlist() {
wishlistContainer.innerHTML = "";
if (wishlist.length === 0) {
wishlistContainer.innerHTML = "<p>No items in wishlist.</p>";
return;
}
wishlist.forEach(product => {
const item = document.createElement('div');
item.className = 'wishlist-item';
item.innerHTML = `
<img src="${product.img}" alt="${product.name}">
<h3>${product.name}</h3>
<p class="price">$${product.price}</p>
<button class="remove-btn" data-id="${product.id}">Remove</button>
`;
wishlistContainer.appendChild(item);
});
// Add remove button logic
document.querySelectorAll('.remove-btn').forEach(btn => {
btn.addEventListener('click', function () {
const id = this.dataset.id;
wishlist = wishlist.filter(p => p.id !== id);
localStorage.setItem('wishlist', JSON.stringify(wishlist));
renderWishlist(); // Refresh UI
});
});
}
renderWishlist();
});