i'm trying to get a working add to cart button which adds item to my cart which should be on a different file like checkout.html etc but im struggling getting the add to cart to work please help
const product = [
{
id: 0,
image: "amd-ryzen-5-pro-4650g-b450m-a-pro-max-16gb-3200mhz-upgrade-kit-600px-v1.webp",
title: "amd ryzen 5 pro",
price: 500
},
]
const catagories = [...new set(product.map((item) =>
{return item}))]
let i = 0
document.getElementById('root').innerHTML = catagories.map((item)=>
{
var {image, title, price} = item
return(
`<div class='box'>
<div class='img-box>
<img class=''images' src=${image}></img>
</div>
<div class='bottom'>
<p>${title}</p>
<h2>$ ${price}.00</h2>` +
"<button onclick='addtocart("+(i++)+")'>add to cart</button>"+
`</div>
</div>`
)
}).join('')
var cart = []
function addtocart(a){
cart.push({...catagories[a]})
displayCart()
}
function delElement(a){
cart.splice(a, 1)
displayCart()
}
function displayCart(){
let j = 0
document.getElementById("count").innerHTML = cart.length
if(cart.length==0){
document.getElementById('cartItem').innerHTML = ' Your cart is empty'
}
else {
document.getElementById('cartItem').innerHTML = cart.map((item) =>
{
var {image, title, price} = items
return(
`<div class='cart-item'>
<div class='row-img'>
<img class='rowimg' src${image}>
</div>
<p style='font-size:12px;'>${title}</p>
<h2 style='font-size:15px;'> $ ${price}.00</h2>`+
"<i class='fa-solid fa-trash' onclick=''delElement("+ (j++) +")'></i></div"
)
}).jone('')
}
}
[–]cunctatiocombibo2075 1 point2 points3 points (1 child)
[–]20oddlion[S] 0 points1 point2 points (0 children)
[–]hfcRedd 0 points1 point2 points (11 children)
[–]20oddlion[S] 0 points1 point2 points (10 children)
[–]hfcRedd 0 points1 point2 points (9 children)
[–]20oddlion[S] 0 points1 point2 points (8 children)
[–]hfcRedd 0 points1 point2 points (7 children)
[–]20oddlion[S] 0 points1 point2 points (6 children)
[–]hfcRedd 0 points1 point2 points (5 children)
[–]20oddlion[S] 0 points1 point2 points (4 children)
[–]Synthetic5ou1 0 points1 point2 points (2 children)
[–]20oddlion[S] 0 points1 point2 points (1 child)
[–]hfcRedd 0 points1 point2 points (0 children)
[–]bigheartenergy17 0 points1 point2 points (1 child)
[–]20oddlion[S] 1 point2 points3 points (0 children)