I'm populating my cart using ajax. Within the cart I've got a quantity field with a plus and minus button. This is the code for the buttons:
<div class="cart-quantity">
<input type='button' value='+' class='qtyplus CartCount' field='updates_{{ item.id }}' />
<input name="updates[]" id="updates_{{ item.id }}" class="quantity CartCount" value="{{ item.quantity }}" />
<input type='button' value='-' class='qtyminus CartCount' field='updates_{{ item.id }}' />
</div>
And here is the javascript to update the quantity field via the plus/minus button:
// This button will increment the value
$('.qtyplus').on("click", function(){
// Stop acting like a button
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[id='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('input[id='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
$('input[id='+fieldName+']').val(0);
}
e.preventDefault();
$(this).closest('form').submit();
});
// This button will decrement the value till 0
$(".qtyminus").on("click",function() {
// Stop acting like a button
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[id='+fieldName+']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 0) {
// Decrement one
$('input[id='+fieldName+']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('input[id='+fieldName+']').val(0);
}
e.preventDefault();
$(this).closest('form').submit();
});
As you can see I've tried changing the click event to on click but it's still not working. Any ideas?