[jQuery] Having trouble with += and toFixed for some reason
Here is my code:
<pre>
<script type="text/javascript">
//on page load
$(document).ready(function() {
//add up the cart totals and display on the page
//setup default values
var bagqty = '';
var bagtotal = '';
//get all table rows
$('#shoppingcart tbody tr').each(function() {
//get the row's price, remove the $
var price = parseFloat($('.itemprice', this).text().replace(/^[^
\d.]*/, ''));
//make sure its a number
price = isNaN(price) ? 0 : price;
//get the row's quantity
var qty = parseInt($('.itemqty', this).text());
//get the item's shipping amount
var ship = parseFloat($('.itemshipping', this).text().replace(/^[^
\d.]*/, ''));
//make sure its a number
ship = isNaN(ship) ? 0 : ship;
//calculate the extended price
var extprice = (qty * price) + ship;
//add back in the $ sign and write to the page
$('.itemextprice', this).text('$' + extprice.toFixed(2));
//add to totals
bagqty += qty;
bagtotal += extprice;
});
//return the totals
$('.bagtotal').text('$' + bagtotal.toFixed(2));
});
</script>
</pre>
I have 2 issues-
1- the bagqty += qty; and bagtotal += extprice; calculations return
appended values, not added. So if there are 2 products and the bagqty
values are 5 and 3 I get 53 instead of 8.
2- the $('.bagtotal').text('$' + bagtotal); line won't work. I get a
bagtotal.toFixed is not a function error. It works fine in the
itemextprize calculation up higher.
Any ideas?