简体   繁体   中英

Shopping Cart with JavaScript

I'm building a shopping cart with Vanilla JavaScript and i'm trying with Console.Log to show price of product "$19.99" when i click button to remove the product, but it's not showing nothing in Console.Log and i think problem is with html but i cannot find it, any idea?

<div class="click-cart">
    <div class="cart-row">
        <span class="cart-item cart-header cart-column">ITEM</span>
        <span class="cart-price cart-header cart-column">PRICE</span>
        <span class="cart-quantity cart-header cart-column">QUANTITY</span></div>
      <div class="olp">
        <div class="lakn">
        <img class="shop-item-cart" src="https://cdn.thewirecutter.com/wp-content/uploads/2018/04/canon-dslrs-march-2018-2x1-lowres3496.jpg">
        <span class="shop-title">Album 1</span>
        <span class="shop-price">$19.99</span>
        <input class="quantity-input" type="number" value="1">
        <button class="delete-cart">X</button>
      </div></div>
        <div class="clear-checkout">
      <button class="checkout">Checkout</button>
      <button class="clear-cart">Clear Cart</button></div>
    <div class="cart-items">
      </div>
      <div class="cart-total">
          <strong class="cart-total-title">Total</strong>
          <span class="cart-total-price">$10.79</span>
      </div>

    for (let i = 0; i < removeCartItemButtons.length; i++) {
    let button = removeCartItemButtons[i]
    button.addEventListener('click', function (event) {
    let buttonCliked = event.target
    buttonCliked.parentElement.parentElement.remove()
    updateCartTotal ()
    })
}

function updateCartTotal () {
    let cartItemContainer = document.querySelector('.click-cart');
    let cartRows = cartItemContainer.querySelector('.cart-row');
    for (let i = 0; i < cartRows.length; i++) {
        let cartRow = cartRows[i]
        let priceElement = cartRow.querySelector('.shop-price');
        let quantityElement = cartRow.querySelector('.quantity-input');
        let price = priceElement.innerText
        console.log(price)
    }
}```
<div class="cart-row">
    <span class="cart-item cart-header cart-column">ITEM</span>
    <span class="cart-price cart-header cart-column">PRICE</span>
    <span class="cart-quantity cart-header cart-column">QUANTITY</span>
</div>

The shop-price element is not in the cart-row div.

Furthermore, your for loop would not run because cartRows is not an array, instead it is a single HTMLElement with a length of undefined , use cartItemContainer.querySelectorAll('.cart-row') to get an interable NodeList instead.

Additionally, since you delete the element before you use the updateCartTotal function it is necessary to have a price variable preset to 0.

function updateCartTotal () {
let cartItemContainer = document.querySelector('.click-cart');
let cartRows = cartItemContainer.querySelectorAll('.cart-row');
let price = 0;
for (let i = 0; i < cartRows.length; i++) {
    let cartRow = cartRows[i]
    let priceElement = cartRow.querySelector('.shop-price');
    let quantityElement = cartRow.querySelector('.quantity-input');
    if (priceElement) {
      price += Number(priceElement.innerText.replace('$',''));
    }
    console.log(price);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM