J.D. J.D. - 1 year ago 110
jQuery Question

Rails 5, Ajax, jQuery - increase number when button clicked

If user clicks "Add to Cart" button, I need to increase the number by one in my navbar div, to show how many products are in his cart.

So, my button has

remote: true

And navbar has a div with id count.

Function I use to increase number:

$('#count').html(function(i, val) { return val*1+1 });

But if I reload page that value disappears. How I can make that value stay there until some action, like 'checkout'?

Or maybe there is a better way for this?

Answer Source

You need to store the cart items to the database. On click of button , an ajax(remote: true) will be fired which will update the cart count in the database & will return the corresponding js.erb file which will update the count in the navbar

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download