Thor Kousted
Thor Kousted

Reputation: 35

How do I change a price when multiple items are selected?

I am doing a project (school) and need help with JavaScript programming. The code can be seen here: https://jsfiddle.net/zvov1jpr/3/

HTML:

<script src="java.js"></script>
<div id="formular">
  <div id="formulartekst">
    <form>

      <h2 class="formskrift">Order Hot Food</h2>
      <p class="kroner">$39 / $29 when 3 or more checked</p>
      <input name="product" value="39" type="checkbox" id="p1" onclick="totalIt()" /> Monday
      <br>
      <input name="product" value="39" type="checkbox" id="p2" onclick="totalIt()" /> Tuesday
      <br>
      <input name="product" value="39" type="checkbox" id="p3" onclick="totalIt()" /> Wednesday
      <br>
      <input name="product" value="39" type="checkbox" id="p4" onclick="totalIt()" /> Thursday
      <br>
      <input name="product" value="39" type="checkbox" id="p5" onclick="totalIt()" /> Friday
      <label>
        <br> Total
        <input value="$0.00" readonly type="text" id="total" />
      </label>
      <input type="submit" value="Order">
    </form>
  </div>
</div>

JavaScript:

function totalIt() {
    var input = document.getElementsByName("product");
    var total = 0;
    for (var i = 0; i < input.length; i++) {
        if (input[i].checked) {
          total += parseFloat(input[i].value);
        }
     }
     document.getElementById("total").value = "$" + total.toFixed(2);
}

When I check boxes it automatically adds up the price (for some reason it doesn't on JSFiddle but it works fine on my website). However, I need it so when I have 3 or more boxes checked, it has to change the price to $29 pr. Check instead of $39.

Upvotes: 1

Views: 1526

Answers (2)

Rajesh
Rajesh

Reputation: 24945

There was a small JSFiddle issue that I have already commented about.

Apart from that you can use querySelectors to reduce code.

JSFiddle

function totalIt() {
  var input = document.querySelectorAll("input[name='product']:checked")
  document.getElementById("total").value = "$" +
    (input.length * (input.length > 2 ? 29 : 39))
}
<script src="java.js"></script>
<div id="formular">
  <div id="formulartekst">
    <form>

      <h2 class="formskrift">Order Hot Food</h2>
      <p class="kroner">$39 / $29 when 3 or more checked</p>
      <input name="product" value="39" type="checkbox" id="p1" onclick="totalIt()" />Monday
      <br>
      <input name="product" value="39" type="checkbox" id="p2" onclick="totalIt()" />Tuesday
      <br>
      <input name="product" value="39" type="checkbox" id="p3" onclick="totalIt()" />Wednesday
      <br>
      <input name="product" value="39" type="checkbox" id="p4" onclick="totalIt()" />Thursday
      <br>
      <input name="product" value="39" type="checkbox" id="p5" onclick="totalIt()" />Friday
      <label>
        <br>Total
        <input value="$0.00" readonly type="text" id="total" />
      </label>
      <input type="submit" value="Order">
    </form>
  </div>
</div>

Upvotes: 1

Himanshu
Himanshu

Reputation: 1002

Change your function to:

function totalIt(){

   var input = document.getElementsByName("product");
   var total = 0;
   for (var i = 0; i < input.length; i++) {
     if (input[i].checked) {
       total+= 1;
     }
   }
   if(total>=3){
   document.getElementById("total").value = "$" + (total*29).toFixed(2);
   }
   else{
   document.getElementById("total").value = "$" + (total*39).toFixed(2);
   }

Upvotes: 0

Related Questions