-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwishlist2.js
More file actions
58 lines (53 loc) · 2.53 KB
/
wishlist2.js
File metadata and controls
58 lines (53 loc) · 2.53 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
document.addEventListener('DOMContentLoaded', function() {
console.log('Document loaded and script is running.');
// Initialize wishlist in localStorage if it doesn't exist
if (!localStorage.getItem('wishlistSpeakers')) {
localStorage.setItem('wishlistSpeakers', JSON.stringify([]));
console.log('Initialized wishlistSpeakers in localStorage.');
}
// Function to add a speaker to the wishlist
function addToWishlist(speakerId) {
let wishlist = JSON.parse(localStorage.getItem('wishlistSpeakers'));
if (!wishlist.includes(speakerId)) {
wishlist.push(speakerId);
localStorage.setItem('wishlistSpeakers', JSON.stringify(wishlist));
console.log(`Added speaker ${speakerId} to wishlist.`);
} else {
console.log(`Speaker ${speakerId} is already in the wishlist.`);
}
}
// Function to remove a speaker from the wishlist
function removeFromWishlist(speakerId) {
let wishlist = JSON.parse(localStorage.getItem('wishlistSpeakers'));
const initialLength = wishlist.length;
wishlist = wishlist.filter(id => id !== speakerId);
if (wishlist.length < initialLength) {
console.log(`Removed speaker ${speakerId} from wishlist.`);
localStorage.setItem('wishlistSpeakers', JSON.stringify(wishlist));
} else {
console.log(`Speaker ${speakerId} was not found in the wishlist.`);
}
}
// Attach click event listeners to "Add to Wishlist" buttons
const addButtons = document.querySelectorAll('.div-sp-sl-wrapper-add');
console.log(`Found ${addButtons.length} 'Add to Wishlist' buttons.`);
addButtons.forEach(button => {
button.addEventListener('click', function(event) {
event.preventDefault();
const speakerId = this.getAttribute('data-speaker-id');
console.log('Add to wishlist clicked:', speakerId);
addToWishlist(speakerId);
});
});
// Attach click event listeners to "Remove from Wishlist" buttons
const removeButtons = document.querySelectorAll('.div-sp-sl-wrapper-remove');
console.log(`Found ${removeButtons.length} 'Remove from Wishlist' buttons.`);
removeButtons.forEach(button => {
button.addEventListener('click', function(event) {
event.preventDefault();
const speakerId = this.getAttribute('data-speaker-id');
console.log('Remove from wishlist clicked:', speakerId);
removeFromWishlist(speakerId);
});
});
});