Search code examples
jquerysyntaxadditionmultiplication

How to change text of span from two different checkbox values


Trying to add a variety of prices together where it multiplied by a multiplier (no of Days) my function works for 100kms but does not produce any results when changed to 200kms, where 100kms is R125/Day and 200kms is R225/day. these are represented in a span ith Daily and total, the daily being the sum of extra's + 100kms and Total being = multiplier * extras + multiplier * 100kms

I've alreadt rewritten my script with the help of a few suggestions but run into another problem of getting the second value to show i.e. 200km value. my Fiddle is here : https://jsfiddle.net/shiataz12/mjnqth3L/169/

I thought of trying something like this:

$("#qr1").click(function() {
displayVals1();
});
$("#qr2").click(function() {
displayVals2();
});

Attempting to get R225/Day and R1125/day as value when 200kms checkbox is checked as base value.


Solution

  • Because I had a moment on my hands I reduced your code to about 15 lines.

    Note, the change of all the inputs is handled with a single function that just redoes all the math each time.

    $(function() {
      $("input.kilo").change(function() {
        $("input.kilo").not(this).prop("checked", false);
      });
      $("input, select").change(function() {
        //get the values of the selected options
        let v = $.map($("input[id^='qr']:checked"), function(i) {
          return parseFloat($(i).val());
        });
        let s = v.reduce((a, b) => a + b); //sum them up to a daily total
        console.log(v);
        $("#userdaily").text(s);
        $("#usertotal").text(s * parseFloat($("#mySelect>option:selected").val()));
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <label for="mySelect">No. Days</label>
    <select name="mySelect" id="mySelect">
      <option name="one" value="1">1</option>
      <option name="two" value="2">2</option>
      <option name="three" value="3">3</option>
      <option name="four" value="4">4</option>
      <option name="five" value="5" selected>5</option>
    </select><br><br>
    <label for="checkbox1">100kms</label>
    <input type="checkbox" name="checkbox1" class="kilo" value="125" id="qr1" checked><br><br>
    <label for="checkbox2">200kms</label>
    <input type="checkbox" name="checkbox2" class="kilo" value="225" id="qr2"><br><br>
    <label for="checkbox3">Tyre</label>
    <input type="checkbox" name="checkbox3" value="20" id="qr3"><br><br>
    <label for="checkbox4">Glass</label>
    <input type="checkbox" name="checkbox4" value="20" id="qr4"><br><br>
    <label for="checkbox5">General</label>
    <input type="checkbox" name="checkbox5" value="60" id="qr5" checked><br><br>
    <label for="checkbox6">Third Party</label>
    <input type="checkbox" name="checkbox6" value="80" id="qr6"><br><br> Daily :<span id="userdaily"> </span><br><br> Total :<span id="usertotal"> </span><br><br>
    <input tyep="text" id="pricef1" name="pricef1" value="">
    <input tyep="text" id="pricef2" name="pricef2" value="">