I manage to capture the 0 that belongs to the total and I get both products in the subtotals tag to do the calculation, for example, if I buy 2 products for 25 it shows me 50, but I want both products and when I press the button it shows me the total in Total box.
For this, I am using the function calculAll
but I am not able to get said value.
Here I paste the js where I only need to obtain the sum of the total of the products. No matter how hard I try, I can't find the key, is it because I have to do two different subtotals? i.e. from each of the subtotals, store the value and then add it?
I want it to do the sum when I click on the event that it creates in the function createProduct
, once I click it shows me the subtotals of the products but it does not make the total sum.
function updateSubtotal(product) {
const price = product.querySelector('.price span')
const priceElement = parseFloat(price.innerText)
const quantity = product.querySelector('.i').value
let quantityElement = quantity
let subtotal = product.querySelector('.subtotal span')
subtotal.innerText = priceElement * quantityElement
console.log(subtotal)
}
function calculateAll(subtotal) {
const firstProduct = document.querySelector('.rubber')
const secondProduct = document.querySelector('.beach')
updateSubtotal(firstProduct) updateSubtotal(secondProduct)
let subTotalPrice = document.querySelectorAll('.subtotal span')
subTotalPrice.forEach((i) => {
let allSubTotalPrice = subTotalPrice[i]
let totalPrice = document.querySelector('.total-value span')
totalPrice.innerText = allSubTotalPrice
function createProduct() {
}
window.addEventListener('load', () => {
const calculatePricesBtn =
document.getElementById('calculate')
calculatePricesBtn.addEventListener('click', calculateAll)
const removeElement = document.getElementById('remove')
removeElement.addEventListener('click', removeProduct)
})
First, I think you have pasted the code wrong, I think the calculateAll function is missing a parenthesis, in addition to the fact that no parameter is sent to it.
Still, in the foreach, if all the subtotals for the products are ok, you can do this:
or I am seeing wrong, or your updateSubtotal function does not have a return (therefore it does not return data (it only shows it on the screen)