I am trying to create a form where I have to calculate values on the basis of values selected. I have three checkboxes which will be given as a choice to user to select and I am showing the total cost in a box. I am trying to achieve the goal through the following logic but its not working at all.
HTML:
<?php
$add_ons = $wpdb->get_results("SELECT add_ons_id, add_ons_name, add_ons_price FROM add_ons;");
foreach($add_ons as $value){
?>
<div class="media">
<div class="media-body">
<div class="form-group">
<div class="checkbox">
<label style="font-weight: bold;"><input class="add-ons" type="checkbox" id="add-ons<?php echo $value->add_ons_id; ?>" data-price="<?php echo $value->add_ons_price; ?>" name="info_status"/><?php echo $value->add_ons_name; ?></label>
</div> <!-- end checkbox -->
</div> <!-- end form group -->
</div>
<div class="media-right" style="vertical-align: middle;">
<span><?php echo '$'. $value->add_ons_price; ?> </span>
</div>
</div><!-- end media -->
<?php } ?>
<div class="media">
<div class="media-body">
Actual Price
</div>
<div class="media-right">
<span class="price-calculated" style="text-decoration: line-through;">$0.00 </span>
</div>
</div>
jQuery
/* calculate the total */
function getTotal(){
var $total = 0.00;
$checkbox = $('input[type="checkbox"]:checked');
$checkbox.each(function(){
$total += parseFloat($(this).attr('data-price')).toFixed(2);
});
return $total;
}
function updateBOX(){
$priceCalculated = $('.price-calculated');
$calculatedPrice = $priceCalculated.html('$' + getTotal());
}
updateBOX();
$checkbox = $('input[type="checkbox"]');
$checkbox.on('change', function(){
updateBOX;
});
Kindly, tell me what I am doing wrong here.
Thank you.