Reputation:
Hi i wrote a jquery script. I'm rookie on jquery. i want to increase cart count +1. But this script didn't add +1 it concats 1. What should i do ?
<script type="text/javascript">
$('#shopping-cart-button').click(function(){
var cart_count = $('#cart_count').text();
cart_count = cart_count + 1;
$('#cart_count').text(cart_count);
});
</script>
Upvotes: 3
Views: 9126
Reputation: 217
try for something like this in case of input type text
$('YOUR_ID_HERE').val($('YOUR_ID_HERE').val() + 'SOME OTHER VALUE');
Upvotes: 0
Reputation: 87073
.text()
returns string, so you have to use parseInt(value, radix)
to convert the string to number.
$('#shopping-cart-button').click(function(){
var basket_count = $('#basket_count').text();
basket_count = parseInt(basket_count, 10) + 1;
$('#basket_count').text(basket_count);
});
Upvotes: 9