cart.js
// Update cart total as items are added and removed from cart
Update cart total as items are added and removed from cart
Update cart total as items are added and removed from cart
function updateCartTotal() {
    var cartItemContainer = document.getElementsByClassName('cart-items')[0]
    cartItemContainer.getElementsByClassName('cart-row')
    var total = 0
    for (var i = 0; i < cartRows.length; i++) {
        var cartRow = cartRows[i]
        var priceElement = cartRow.getElementsByClassName('cart-price')[0]
        var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')
        [0]
        var price = paseFloat(priceElement.innerText.replace('$', ''))
        var quantity = quantityElement.value
        console.log(price * quantity)
        total = total + (price * quantity)
    }
    total = Math.round(total *100) / 100
    document.getElementsByClassName('cart-total-price')[0].innerText = '$' + total
}
function updateCartTotal ( ) { var cartItemContainer = document . getElementsByClassName ( 'cart-items' ) [ 0 ] cartItemContainer . getElementsByClassName ( 'cart-row' ) var total = 0 for ( var i = 0 ; i < cartRows . length ; i ++ ) { var cartRow = cartRows [ i ] var priceElement = cartRow . getElementsByClassName ( 'cart-price' ) [ 0 ] var quantityElement = cartRow . getElementsByClassName ( 'cart-quantity-input' ) [ 0 ] var price = paseFloat ( priceElement . innerText . replace ( '$' , '' ) ) var quantity = quantityElement . value console . log ( price * quantity ) total = total + ( price * quantity ) } total = Math . round ( total * 100 ) / 100 document . getElementsByClassName ( 'cart-total-price' ) [ 0 ] . innerText = '$' + total }
Update cart total as items are added and removed from cart
Update cart total as items are added and removed from cart
// Ensures total does not have more than 2 decimal places (formats in currrency)