Calculate the total cost of the products within the shopping cart and displaying it at the bottom rather than manual input

后端 未结 2 1400
误落风尘
误落风尘 2021-01-29 04:48

I have made a checkout form that displays the cost of a product, the subtotal, GST cost, delivery cost, and total price. What id like to do is take the cost of each product whic

2条回答
  •  粉色の甜心
    2021-01-29 05:22

    you can use querySelectorAll

    for example:

    // Get all the elements that have the `.price` class
    const prices = document.querySelectorAll(".price");
    

    Then you could loop over prices use array.reduce()

    // convert the nodeList into an array and loop over it
    let total = [...prices].reduce((acc, price) => {
        return acc + parseFloat(price.innerHTML);  
    }, 0);
    

    This would however require you to edit the HTML

    From

    $429.00 (GST Inc)

    To something like -

    $
    429.00
    (GST Inc)

    or you could use a regular expression on the price.innerHTML

提交回复
热议问题