0

I have three different product arrays, here's one:

let necklaces = [
  {
    id: "BlueHeartNecklace",
    cat: "necklaces",
    price: "$25",
    name: "Blue Heart Necklace",
    img: "images/custom_jewlery/necklaces/61-t5HCxAEL._AC_UL480_QL65_.jpg",
    orders: 0,
  },
  {
    id: "BlueTileNecklace",
    cat: "necklaces",
    price: "$14",
    name: "Blue Tile Necklace",
    img: "images/custom_jewlery/necklaces/71pYceJZEgL._AC_UL480_QL65_.jpg",
    orders: 0,
  },
  {
    id: "GreenPearNecklace",
    cat: "necklaces",
    price: "$32",
    name: "Green Pear Necklace",
    img: "images/custom_jewlery/necklaces/61BHPeEKb7L._AC_UL480_QL65_.jpg",
    orders: 0,
  },
];

Every time I add one of the products to the cart, it shows me the last product in the array. here's the code to display the array:

let necklace;
function showNecklaces() {
  display.innerHTML = "";
  for (let index = 0; index < necklaces.length; index++) {
    necklace = necklaces[index];
    display.innerHTML += `<div ${necklaces[index].name}>
            ${necklaces[index].name}  , ${necklaces[index].price} ,  
            <img src="${necklaces[index].img}"></div>
            <button class="btn btn-primary" onclick='add(necklace)'> Add to cart</button> 
            `;
  }
}

Here's the function where I add products to the cart:

let cart = [];
function add(product) {
  console.log(product);
  cart.push(product);
}

And here's the function where I click when I want to have the whole cart displayed:

function showCart() {
  display.innerHTML = "";
  for (let index = 0; index < cart.length; index++) {
    display.innerHTML += `<div ${cart[index].name}>
    <button class="btn btn-danger" onClick='remove(${index})'>Remove</button>;
        ${cart[index].name}  , ${cart[index].price} ,
        <img src="${cart[index].img}"></div>`;
  }
}
AYB
  • 3
  • 2
  • 4
    You forgot to ask a question – Jamiec Jun 09 '21 at 09:11
  • Good display of your code, but what exactly is the problem? – isaacsan 123 Jun 09 '21 at 09:11
  • Inline event handlers like `onclick` are [not recommended](/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](/a/43459991/4642212) way of registering events. Always [use `addEventListener`](//developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. Your `necklace` falls into this problem: [JavaScript closure inside loops – simple practical example](/q/750486/4642212) – Sebastian Simon Jun 09 '21 at 09:11
  • 1
    Use [event delegation](//developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_delegation) instead of assigning multiple event listeners — it’s more maintainable, and applies to dynamically added elements. E.g., use an [event argument](//developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#The_event_listener_callback)’s [`target`](//developer.mozilla.org/en-US/docs/Web/API/Event/target). See [the tag info](/tags/event-delegation/info) and [What is DOM Event delegation?](/q/1687296/4642212). – Sebastian Simon Jun 09 '21 at 09:12
  • The problem is that it displays the last product in the array, not the product that I'm selecting. My question is: where is the problem? And how do I fix it? – AYB Jun 09 '21 at 09:17
  • See Sebastian's comments above. @AYB - that is the solution – Jamiec Jun 09 '21 at 09:27
  • I looked at Sebastian's comment, I'm having a difficult time understanding what I'm supposed to do and where – AYB Jun 09 '21 at 10:31

0 Answers0