forked from Anushkabh/krishiconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toast.js
44 lines (36 loc) · 1.54 KB
/
toast.js
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
// document.getElementById("toast-btn").addEventListener("click", function () {
// // Show success toast
// Toastify({
// text: "Product added to cart!",
// duration: 3000,
// gravity: "bottom",
// position: "right",
// backgroundColor: "rgba(0,128,0,0.8)",
// }).showToast();
// });
//added this
document.addEventListener("DOMContentLoaded", function() {
// Select all elements with the class 'add-to-cart-btn'
const addToCartButtons = document.querySelectorAll(".add-to-cart-btn");
// Loop through each button and add an event listener
addToCartButtons.forEach(button => {
// Check if the event listener is already attached
if (!button.dataset.listenerAttached) {
button.addEventListener("click", function(event) {
// Get product name and price from button attributes
const productName = event.target.getAttribute("data-product-name");
const productPrice = event.target.getAttribute("data-product-price");
// Add product to cart (implement your own logic here)
addToCart(productName, productPrice);
alert(`${productName} added to cart successfully!!`);
});
// Mark the button to indicate that the event listener is attached
button.dataset.listenerAttached = "true";
}
});
// Example addToCart function (implement your own logic)
function addToCart(product, price) {
console.log(`Product added to cart: ${product}, Price: ${price}`);
// Add your cart logic here
}
});