0

When I click on the checkbox at the top, it puts a '0' in the total box, so I know that it is connected correctly, however I think there is a problem in the logic in the loop. One of the elements in html looks like this.

const form = document.getElementById('bookingForm');
const total = document.getElementById('total');
const checkboxes = document.querySelectorAll('input[data-price][type=checkbox]');
const cbamount = checkboxes.length;

document.getElementsByName('event[]')[0].onclick = function() {
  totalPrice()
};

function totalPrice() {
  let totalprice = 0;
  for (let i = 0; i < cbamount; i++) {
    const box = checkboxes[i];
    if (box.checked) {
      box.dataset.price = totalprice + box.dataset.price;
    } //if
  } //for

  document.getElementsByName("total")[0].value = totalprice;
}
<span class="eventTitle">Carmen </span>
<span class="eventStartDate">2020</span>
<span class="eventEndDate">2020</span>
<span class="catDesc">T</span>
<span class="venueName">Mill </span>
<span class="eventPrice">3</span>
<span class="chosen"><input type="checkbox" name="event[]" value="11" data-price="35.00"></span>


<section id="Cost">
<h3>Total</h3>
 Total <input type="text" name="total" size="20" readonly="">
</section>
cacowa1
  • 21
  • 1
  • 5
  • _"however I think there is a problem in the logic in the loop"_ - Why do you think so? – Andreas Jan 01 '20 at 11:31
  • Because the total in the box does not change from 0. It makes me think that the total is not getting added up correctly. – cacowa1 Jan 01 '20 at 11:32
  • 1
    Don't _"think"_... Start to [debug](https://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Andreas Jan 01 '20 at 11:32
  • I am, but its not something that comes up on a debugger, as I am missing something. Is there anything that stands out? – cacowa1 Jan 01 '20 at 12:07
  • You are not adding anything to `totalprice`. You only update the `box.dataset.price`. That line should probably be `totalprice = totalprice + parseFloat(box.dataset.price);` – Gabriele Petrioli Jan 01 '20 at 12:28
  • I made you a snippet. It is not complete. It really helps when you post a [mcve]. I have however written an answer based on the code you have so far. – mplungjan Jan 01 '20 at 13:51
  • PLEASE post ALL relevant HTML. That means a COMPLETE example with AT LEAST TWO items but ALSO with the TOTAL field. See my code is working on what I have gotten in bits and pieces but we CANNOT help you unless you post a [mcve]!!!!! The code above does not match your questions – mplungjan Jan 02 '20 at 10:11

1 Answers1

1

You have no total in the code you provided.

I would personally use ID when only having one element and if more, use relative addressing and/or delegation

const form = document.getElementById('booking');
const total = document.getElementById('total');
document.getElementById("booking").addEventListener("click", function(e) {
  if (e.target.name === "event[]") {
    let totalprice = 0;
    [...document.querySelectorAll('input[data-price][type=checkbox]')].forEach(function(box) {
      if (box.checked) {
        totalprice += +box.dataset.price;
      } //if
    })
    document.querySelector("[name=total]").value = totalprice.toFixed(2);
  }
})
<form id="booking" method="get">
  <section id="book">
    <h2>Select Events</h2>

    <div class="item">
      <span class="eventTitle">Carmen </span>
      <span class="eventStartDate">2020</span>
      <span class="eventEndDate">2020</span>
      <span class="catDesc">T</span>
      <span class="venueName">Mill </span>
      <span class="eventPrice">3</span>
      <span class="chosen"><input name="event[]" type="checkbox" value="11" data-price="35.00"></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="10.00"></span>
    </div>
  </section>

  <section id="Cost">
    <h3>Total</h3>
    Total <input type="text" name="total" size="20" readonly="">
  </section>
</form>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • This is the functionality that I want, however on my site it does not update the text box. It remains empty and the console logs no errors. I appreciate the help, but I still cant get it to work. – cacowa1 Jan 01 '20 at 13:57
  • Please post more HTML – mplungjan Jan 01 '20 at 13:58
  • Il put it in the original post. – cacowa1 Jan 01 '20 at 14:02
  • @ccowan1 Please do – mplungjan Jan 01 '20 at 14:16
  • My thread got closed, I have created a new one with updated HTML. Il accept your answer on this thread for your help. – cacowa1 Jan 01 '20 at 14:34
  • 1
    No need to create a new thread. I can still update my answer here – mplungjan Jan 01 '20 at 14:45
  • PLEASE give more info here. For example my code is using id="total" – mplungjan Jan 02 '20 at 10:07
  • @ccowan1 Why does ASH not have a price? – mplungjan Jan 02 '20 at 10:13
  • Sorry for not giving much information, and for posting lots of questions. Im a student and have been stuck on this for almost a week. Im trying to not give out too much information about it, the main task is the JavaScript so I have not been worried about the html. Apologies if this has made me look cagey etc, i dont mean to be, I was just trying to get ideas on how to fix this myself. – cacowa1 Jan 02 '20 at 10:17
  • I will update my post with more HTML, what sections do you need? total and form? – cacowa1 Jan 02 '20 at 10:17
  • I need the form with at least two complete items plus the total you are trying to update. As you can see I have NO IDEA why you cannot get my code to work! Do you have errors in the console? – mplungjan Jan 02 '20 at 10:22
  • Okay, i'll update that now. No, I dont have any errors in the console, which is the frustrating thing. The function executes, as the totalprice gets posted into the total price box. It seems like it doesnt execute the for loop correctly though. – cacowa1 Jan 02 '20 at 10:28
  • @cacowa1 updated my code to use name instead of ID - My original code would have worked if you had added id="total" to your html field – mplungjan Jan 02 '20 at 12:38
  • Can I ask a question about how would I get querySelector() to select all divs with class Item? Because i'm not sure as to why my original code didnt work. I had 'const items = form.querySelectorAll('div.item');' but not sure why this didn't pick up all the items? I really do appreciate the help by the way – cacowa1 Jan 02 '20 at 12:45
  • Why do you need the items when you select the checkboxes? But to answer your question, `document.querySelectorAll(".item")` – mplungjan Jan 02 '20 at 12:48
  • @cacowa1 PS: Please delete the other questions and we continue here – mplungjan Jan 02 '20 at 13:21