CODE
Here's the code for a function which is triggered when a person clicks on "Add to Cart" button. It creates a row inside the cart using the data from localStorage about the items selected by the user from the menu.
function addItemToCart() {
var cartRow = document.createElement("div");
cartRow.classList.add("cart-row");
var cartItems = document.getElementsByClassName("cart-items")[0];
var cartItemNames = cartItems.getElementsByClassName("cart-item-title");
//Putting the data
var locStore = JSON.parse(localStorage.getItem("selectedProduct"));
locStore.forEach((item) => {
var cartRowContents = `
<div class="cart-item cart-column">
<img class="cart-item-image" src="${item.image}" width="100" height="100">
<span class="cart-item-title">${item.title}</span>
<span class="cart-item-size">"Rs.${item.sizePrice}"</span>
</div>
<span class="cart-price cart-column">${item.price}</span>
<div class="cart-quantity cart-column">
<input class="cart-quantity-input" type="number" value="1">
<button class="btn btn-danger" type="button">REMOVE</button>
</div>`;
});
cartRow.innerHTML = cartRowContents;
cartItems.append(cartRow);
cartRow
.getElementsByClassName("btn-danger")[0]
.addEventListener("click", removeCartItem);
cartRow
.getElementsByClassName("cart-quantity-input")[0]
.addEventListener("change", quantityChanged);
}
ERROR
I'm getting the error
Uncaught ReferenceError: cartRowContents is not defined
I have defined cartRowContents. I also tried defining it using 'let' but the error persisted.
I'm fetching the data from localStorage and rendering it to the UI using above method.
Please help.