Auto calculate input values

I’m trying to calculate several input values together using jQuery. I’ve managed to make it work apart from the number contained within <input type="number" class="booking-input"/>. It just will not add to the grand total!

Here is my function:

  function checkChecked() {
  
    var check_val = $('.ck-button input:checked').length;
    var priceEvent = "<?php echo $pricePer; ?>";

    var numberVal = $('.booking-input').val();

if(check_val == "") {
var check_val = numberVal;
}


if($('#bcDesign').is(':checked')) {
var design = 90;
}else{
var design = 0;
}



var totalVal = check_val * priceEvent + design;
var stripeTotalVal = totalVal * 100;

$("#totalPrice").html(commaSeparateNumber(totalVal));
$('#grandTotal').val(stripeTotalVal);

  }

By wrapping it inside $(".booking-input:input, .ck-button input:checked, #bcDesign, #myDesign").change(function () { I can get it to work, however when my form has an error (i.e. a required input box is empty) the total value of course goes back to 0 and will not update until something gets “changed”.

Any help on this one would be appreciated!

Can you provide a runnable example that demonstrates your issue. A JSFiddle would be quite useful, for example.

I would like to ask what you intend the following to achieve:

if (check_val == "") {
    var check_val = numberVal;
}

Because currently, nothing happens if checkboxes are checked, and if none are checked then that booking value replaces the number of checked checkboxes - which doesn’t seem to be appropriate.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.