Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Addition Operation

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>

1 Answers

.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);
});
like image 150
thecodeparadox Avatar answered Mar 14 '26 12:03

thecodeparadox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!