Still learning JQuery. Still new to JQuery in fact. Trying to code is:
When the an items checkbox is checked, it calculates the service fee times years. then add the license fee to it. Finally it takes the total and times the quantity, which in then creates the item_cost.
If more than one item is checked, it will also calculate the item_cost and adds each checked item's cost, creating a total_item_cost. And, it will add each checked item's training hours, creating a total_training_hours.
Once item is done calculating, it will then display the item name, years, quantity, training hours, and total cost of each item that is checked at the bottom in the table. It will also display another table with total training hours and the the total cost of all items.
I have coded below but cannot seem to get the items to show in the table.
<script>
$(document).ready(function(){
$('#add_item_here').hide();
$('#add_totals_here').hide();
$('input[id^=item_add]').change(function(){
var index = $(this).attr('id').replace('item_add','');
if($(this).is(':checked')){
$('#add_item_here').show();
$('#add_totals_here').show();
var item_cost = 0;
var total_cost = 0;
$('input:checkbox:checked').each(function(){
item_cost+=parseInt($('#servicefee'+index).val());
item_cost*=parseInt($('#yrs'+index).val());
item_cost+=parseInt($('#licensefee'+index).val());
item_cost*=parseInt($('#qty'+index).val());
var item = $('#item'+index).val();
var traininghrs = parseInt($('#traininghrs'+index).val());
var qty = parseInt($('#qty'+index).val());
var yrs = parseInt($('#yrs'+index).val());
});
var total_cost = item_cost;
$('input:checkbox:checked').each(function(){
total_cost+= item_cost;
});
var total_training = 0;
$('input:checkbox:checked').each(function(){
total_training+=parseInt($('#traininghrs'+index).val());
});
}
$('#add_item_here > tbody:last-child').append('' + item +'' + yrs +'' + qty +'' + traininghrs + ''+ item_cost + '');
$('#add_totals_here > tbody:last-child').append('' + total_training + ''+ total_cost + '');
$('#qty'+index).change(function(){
$('input:checkbox').trigger('change');
});
$('#yrs'+index).change(function(){
$('input:checkbox').trigger('change');
});
});
});
</script>
<center>
<table id="add_item_here" style="width: 98%"> <tbody> <tr><td>Item</td><td>Years</td><td>Quantity</td><td>Training Hours</td><td>Total Item Cost</td></tr> </tbody> </table>
</center>
<center>
<table id="add_totals_here" style="width: 98%"> <tbody> <tr><td></td><td>Total Cost</td></tr> </tbody> </table>
</center>
What I have tried:
fixed the iteration.
updated the append. Still looking for answers