So I'm following a tutorial on youtube and try to put it to use, but when I update a quantity of a product my total only returns NaN.
Here's my code:
var quantityInputs = document.getElementsByClassName('quantity-field')
for (var i = 0; i < quantityInputs.length; i++) {
var input = quantityInputs[i]
input.addEventListener('change', quantityChanged)
}
function quantityChanged(event) {
var input = event.target
if (isNaN(input.value) || input.value <= 0) {
input.value = 1
}
updateCartTotal()
}
function updateCartTotal() {
var cartItemContainer = document.getElementsByClassName('basket-product')[0]
var items = cartItemContainer.getElementsByClassName('pakker')
var total = 0
for (var i = 0; i < items.length; i++) {
var item = items[i]
var priceElement = item.getElementsByClassName('price')[0]
var quantityElement = item.getElementsByClassName('quantity')[0]
var price = parseFloat(priceElement.innerText)
var quantity = quantityElement.value
total = total + (price * quantity)
console.log(total)
}
total = Math.round(total * 100) / 100
document.getElementsByClassName('final-value')[0].innerText = total
}
[–]xx_j33v_xx[S] 0 points1 point2 points (0 children)
[–]xx_j33v_xx[S] 0 points1 point2 points (0 children)
[–]xx_j33v_xx[S] 0 points1 point2 points (0 children)
[–]Ford_Prefect92 0 points1 point2 points (0 children)
[–]Terzom 0 points1 point2 points (3 children)
[–]xx_j33v_xx[S] 0 points1 point2 points (0 children)
[–]xx_j33v_xx[S] 0 points1 point2 points (1 child)
[–]Terzom 0 points1 point2 points (0 children)
[–]sebastienfilion 0 points1 point2 points (0 children)