Reputation: 47
I am using this code I need to add together any inputs that are checked price values. They price values are dynamically generated. but for the purpose of being on here they have a value. Any help would be greatly appreciated. Thanks.
var $variantTitle = $('.variant_title');
var $variantPrice = $('.variant_price');
$('#products :checkbox').on('change', function() {
if (this.checked) {
var $row = $(this).closest('tr');
var title = $row.find('.title').text();
var price = $row.find('.price').text();
$variantTitle.text(title);
$variantPrice.text(price);
} else {
$variantTitle.empty();
$variantPrice.empty();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="products">
<tbody>
<tr>
<td>
<span class="variant_title"></span>
</td>
</tr>
<tr>
<td><input name="updates[31435395282]" type="checkbox" value="31435395282:" /> </td>
<td>Hoodie - Unisex Hoodie / Black / S - <span class="price">$0.01</span></td>
<td class="title hide">Unisex Hoodie / Black / S - $0.01</td>
</tr>
<tr>
<td><input name="updates[31435395346]" type="checkbox" value="31435395282:" /> </td>
<td>Hoodie - Unisex Hoodie / Black / M - <span class="price">$0.01</span></td>
<td class="title hide">Unisex Hoodie / Black / M - $0.01</td>
</tr>
<tr>
<td><input name="updates[31435395410]" type="checkbox" value="31435395282:" /> </td>
<td>Hoodie - Unisex Hoodie / Black / L - <span class="price">$0.01</span></td>
<td class="title hide">Unisex Hoodie / Black / L - $0.01</td>
</tr>
</tbody>
</table>
<div class="variant_price"></div>
Upvotes: 1
Views: 53
Reputation: 11342
A simple example, on each click, call a .each
function to loop through all checkbox and if the checkbox is checked add it to the total price, at the end output total price.
Here
+($(this).parent().next('td').find('.price').text().replace(/\$/g, ''));
Find the checked price use $(this).parent().next('td').find('.price')
Then .text().replace(/\$/g, ''));
to find the price and replace the $
+
will force the variable to become number, otherwise you are just concating string like 0.010.01 instead of 0.02.
var $variantTitle = $('.variant_title');
var $variantPrice = $('.variant_price');
$('#products :checkbox').on('change', function() {
if (this.checked) {
var $row = $(this).closest('tr');
var title = $row.find('.title').text();
var price = $row.find('.price').text();
$variantTitle.text(title);
$variantPrice.text(price);
} else {
$variantTitle.empty();
$variantPrice.empty();
}
//handling total price
var total = 0;
$('#products :checkbox').each(function(){
if(this.checked){
total += +($(this).parent().next('td').find('.price').text().replace(/\$/g, ''));
}
})
//output your total price
$('#total_price').text(total);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="products">
<tbody>
<tr>
<td>
<span class="variant_title"></span>
</td>
</tr>
<tr>
<td><input name="updates[31435395282]" type="checkbox" value="31435395282:" /> </td>
<td>Hoodie - Unisex Hoodie / Black / S - <span class="price">$0.01</span></td>
<td class="title hide">Unisex Hoodie / Black / S - $0.01</td>
</tr>
<tr>
<td><input name="updates[31435395346]" type="checkbox" value="31435395282:" /> </td>
<td>Hoodie - Unisex Hoodie / Black / M - <span class="price">$0.01</span></td>
<td class="title hide">Unisex Hoodie / Black / M - $0.01</td>
</tr>
<tr>
<td><input name="updates[31435395410]" type="checkbox" value="31435395282:" /> </td>
<td>Hoodie - Unisex Hoodie / Black / L - <span class="price">$0.01</span></td>
<td class="title hide">Unisex Hoodie / Black / L - $0.01</td>
</tr>
</tbody>
</table>
<div class="variant_price"></div>
<div>Total Price: $<span id="total_price">0</span></div>
Upvotes: 1