I need it to connect my code to the HTML and loop through each of the items.
There are more items to loop through. I can't see a problem in my loop, and I have created others which work perfectly on different websites. Basically I need to add up the data-price if the checkbox is checked and put the total in a total box. The total box, which is what totalPrice
is, has connected fine.
function totalPrice() {
let totalprice = 5;
const items = form.querySelectorAll('section.book');
const cbamount = items.length;
for (let i = 0; i < cbamount; i++) {
const checkboxes = document.querySelectorAll('input[data-price][type=checkbox]');
const box = checkboxes[i];
if (box.checked) {
totalprice = totalprice + parseFloat(box.dataset.price);
alert(totalprice);
} //if
} //for
document.getElementsByName("total")[0].value = totalprice;
}
<form id="booking" method="get">
<section id="book">
<h2>Select Events</h2>
<div class="item">
<span class="eventTitle">A Chr</span>
<span class="eventStartDate">202</span>
<span class="eventEnd">20</span>
<span class="catD">Fam</span>
<span class="venueNa">PLAYy</span>
<span class="eventPr">1</span>
<span class="chosen"><input type="checkbox" name="event[]" value="6" data-price="18.50"></span>
</div>
<div class="item">
<span class="eventTitle">Ash</span>
<span class="eventStartDate">202</span>
<span class="eventEnd">2020-12-31</span>
<span class="catD">Exhib</span>
<span class="venueNa">The Biy</span>
<span class="eventPr">0.00</span>
<span class="chosen"><input type="checkbox" name="event[]" value="17" data-price="0.00"></span>
</div>
</section>