Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
how to reuse a variable with everything and its value in another page of my project

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;

my files in vs code

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

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()
    }
about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!