I have seen a few examples for jquery and adding column values; however, the examples had only one column and one value.
I wanted to know if you had multiple columns then how do you grab specific ones?
For example, if I want to add all of the Quantity columns together or multiply Cost column with its adjacent Quantity?
So far I have the beginning set up but it doesn't work. Initially I tried
child_nodes.eq(2).text()
to get the column of quantity but I know my logic is off.
$(document).ready(function() {
$('#checkout').on('click', function() {
item_count();
});
});
function item_count(){
$('.product').each(function() {
//find number of child nodes
var child_nodes = $(this).text(); //children returns all
var count = "";
for (var i=0; i<child_nodes.length; i++){
//whole row output
count += child_nodes.eq(i).text() + "\n";
}
});
//var total = parseInt($('.product').val());
var total += parseInt(count);
//update value
return $('#nitems').text(total);
};
I just started trying j query so I feel like my code looks very noobish so I analogize.
<h4>Shopping Cart</h4>
<table class='table'>
<thead>
<tr>
<th>Product</th><th>Unit cost</th><th>Quantity</th>
</tr>
</thead>
<tbody>
<tr class='product'>
<td>Milk Chocolate</td>
<td>7.48</td>
<td>5</td>
</tr>
<tr class='product'>
<td>Assorted Fine Chocolates</td>
<td>9.98</td>
<td>7</td>
</tr>
<tr class='product'>
<td>Assorted Milk & Dark Chocolates</td>
<td>12.98</td>
<td>9<td>
</tr>
<tr class='product'>
<td>Assorted Dessert Truffles</td>
<td>15.98</td>
<td>4</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class='row'>
<div class='col-md-4'>
<h3>Finalize Sale </h3>
<table class='table'>
<tr>
<td>Total Items</td>
<td><span id="nitems" >0</td>
</tr>
<tr>
<td>Subtotal</td>
<td><span id="subtotal" >0</td>
</tr>
<tr>
<td>5% Sales tax</td>
<td><span id="tax" >0</td>
</tr>
<tr>
<td>Total</td>
<td><span id="total" >0</td>
</tr>
<tr>
<td>Final amount (with 15% discount)</td>
<td><span id="final" >0</td>
</tr>
</table>