WED201c_-_SP_2023_-_PE_-_01_138.webp
minhthien143

WED201c_-_SP_2023_-_PE_-_01_138.webp

js > Js mycode.js > calcTotal
1
const orderBtn = document.querySelector(".order-button")
2
3
const orderedListContent = document.querySelector('.ordered-list-content')
4
5
const items = document.querySelectorAll('.list-item-container')
6
const orderedListContainer = document.querySelector('.ordered-list-container')
7
8
9
= orderedListContent.innerHTML
10
11
12
13
orderBtn.addEventListener("click", () => {
const existingTotalAmountBanner = document.querySelector('.total-amount-banner')
if (existingTotalAmountBanner) {
orderedListContainer.removeChild(existing TotalAmountBanner)
14
}
15
16
17
18
19
20
21
22
23
24
25
26
const selectedItemsPrices = []
items.forEach((item) => {
const checkbox = item.querySelector('input')
if (checkbox.checked) {
const newItem = document.createElement('p')
newItem.textContent = checkbox.name
orderedListContent.appendChild(newItem)
const price = item.querySelector('.item-price')
const priceInt = parseFloat(price.textContent.replace('$', ''))
selectedItemsPrices.push(priceInt)
27
}
28
29
})
30
calcTotal(selectedItemsPrices)
31
})
32
33 function calcTotal(itemPrices) {
34
if (itemPrices.length > 0) {
35
let sum = 0;
36
for (let i = 0; i < itemPrices.length; i++) {
37
sum += itemPrices[i]
38
}
39
const totalAmount Banner = document.createElement('div')
40
totalAmountBanner.className = 'total-amount-banner'
41
totalAmountBanner.textContent = "Total amount: + sum.toString() + '$'
42
orderedListContainer.appendChild(totalAmountBanner)
43
}
Chưa có bình luận nào.

Thông tin

Category
WED201c
Thêm bởi
minhthien143
Ngày thêm
Lượt xem
71
Lượt bình luận
0
Rating
0.00 star(s) 0 đánh giá

Share this media

Back
Bên trên Bottom