-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwishlist.js
More file actions
64 lines (52 loc) · 2.19 KB
/
wishlist.js
File metadata and controls
64 lines (52 loc) · 2.19 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
59
60
61
62
63
64
document.addEventListener('DOMContentLoaded', function () {
const wishAdd = document.querySelectorAll('.speaker-wish-g.add');
const wishRemove = document.querySelectorAll('.speaker-wish-g.remove');
// Function to update the wish list count
function updateWishListCount() {
// Count only keys that match a specific pattern to ensure they belong to the wish list
const count = Object.keys(localStorage).filter(key => key.startsWith('wish-name-')).length;
document.querySelector('.hb-count-speaker').textContent = count;
}
// Initialize wish list count on load
updateWishListCount();
wishAdd.forEach((add, idx) => {
const key = `wish-name-${idx + 1}`;
add.setAttribute('data-key', key);
// Fallback if data attributes are not set
const name = add.dataset.name || 'Unknown';
const lastname = add.dataset.lastname || 'Name';
const fullName = `${name} ${lastname}`;
const nextRemove = add.nextElementSibling;
add.addEventListener('click', (e) => {
e.preventDefault();
add.style.display = 'none';
nextRemove.style.display = 'block';
if (typeof (Storage) !== "undefined") {
localStorage.setItem(key, fullName);
updateWishListCount();
} else {
alert("Sorry, your browser does not support Web Storage...");
}
});
if (localStorage.getItem(key)) {
add.style.display = 'none';
nextRemove.style.display = 'block';
}
});
wishRemove.forEach((remove, idx) => {
const key = `wish-name-${idx + 1}`;
remove.setAttribute('data-key', key);
const addPrev = remove.previousElementSibling;
remove.addEventListener('click', (e) => {
e.preventDefault();
remove.style.display = 'none';
addPrev.style.display = 'block';
if (typeof (Storage) !== "undefined") {
localStorage.removeItem(key);
updateWishListCount();
} else {
alert("Sorry, your browser does not support Web Storage...");
}
});
});
});