-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshop.js
More file actions
39 lines (32 loc) · 1.29 KB
/
shop.js
File metadata and controls
39 lines (32 loc) · 1.29 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
38
39
document.addEventListener('DOMContentLoaded', () => {
const likeButtons = document.querySelectorAll('.like-btn');
likeButtons.forEach(button => {
button.addEventListener('click', function (e) {
e.preventDefault();
const icon = this.querySelector('i');
const productId = this.dataset.id;
const name = this.dataset.name;
const price = this.dataset.price;
const img = this.dataset.img;
// Toggle heart color
icon.classList.toggle('active-heart');
// Load existing wishlist
let wishlist = JSON.parse(localStorage.getItem('wishlist')) || [];
const existing = wishlist.find(item => item.id === productId);
if (existing) {
// If already liked, remove it
wishlist = wishlist.filter(item => item.id !== productId);
} else {
// Add new item
wishlist.push({
id: productId,
name: name,
price: price,
img: img
});
}
// Save updated wishlist
localStorage.setItem('wishlist', JSON.stringify(wishlist));
});
});
});