im working on a e-commerce and im trying to do the page where you fill your info, but i cant use my variable cart, or my total.textContent i just need to show my cart, the total of the cart and my form but i cant use any of my variables or functions
enter code here
const container = document.getElementById("container");
const cartContainer = document.getElementById("cart-items");
let cart = [];
export let stock = [
{id: 1, item: "Playera blanca - Logo rojo", precio: 265, imagen: '<img class="cart-review-img" src="./images/Playera blanca - Logo rojo.png" alt="">'
}];
const upDateCart = () => {
cartContainer.innerHTML = ""
cart.forEach((prod) => {
const div = document.createElement("div")
div.className = ("cart-review-cotainer")
div.innerHTML = `
<div class="cart-image col-md-5">
${prod.imagen}
</div>
<div class="col-md-5 d-flex flex-column justify-content-between">
<div>
<p id="cart-item-name">${prod.item}</p>
<p id="cart-item-price">${prod.precio}</p>
</div>
</div>
<div class="col-md-2 delete-container">
<button onclick="deleteItem(${prod.id})" id="delete-item"><img class="delete-img" src="./images/trash-solid.svg" alt=""></button>
</div>
`
cartContainer.appendChild(div)
localStorage.setItem("newCart", JSON.stringify(cart))
})
total.innerText = cart.reduce((acc, prod) => acc + prod.precio, 0)
}
if(localStorage.getItem("newCart")) {
newCart = JSON.parse(localStorage.getItem("newCart"))
console.log(newCart)
upDateCart()
}
window.deleteItem = deleteItem;
window.cart = cart;
window.test = test;
window.newCart = newCart;
I couldn't run your snippet, but I guess
if(localStorage.getItem("newCart")) {
newCart = JSON.parse(localStorage.getItem("newCart"))
console.log(newCart)
cart = newCart; // 👈 add this line maybe?
upDateCart()
}