Ad

JS Incrementing Quantity 2 By 2 Instead Of 1 By 1

- 1 answer

I'm creating a shopify theme but when i add this script to carti got a bug. Whe you try increment products in cart they jump 2 by 2.

HTML

<div class="quantity">

<input type="number" name="updates[]" value="{{ item.quantity }}" data-id="{{ item.id }}" min="0" max="99" step="1" title="Qty" class="qty" size="4">

</div>

JS

if ((".quantity").length > 0) {
    var form_cart = $("form .quantity");
    form_cart.prepend('<span class="minus"><i class="fa fa-minus-square-o"></i></span>');
    form_cart.append('<span class="plus"><i class="fa fa-plus-square-o"></i></span>');

    var minus = form_cart.find($(".minus"));
    var plus  = form_cart.find($(".plus"));

    minus.on("click", function(){
      var qty = $(this).parent().find(".qty");
      if (qty.val() <= 1) {
        qty.val(1);
      } else {
        qty.val((parseInt(qty.val())-1));
      }
    });
    plus.on("click", function(){
      var qty = $(this).parent().find(".qty");
      qty.val((parseInt(qty.val())+1));
    });
  }

ps. min and max arent working

sorry for bad english

Ad

Answer

changing var qty = $(this).parent().find(".qty"); for outside if and works tks for all

if ((".quantity").length > 0) { var form_cart = $("form .quantity"); form_cart.prepend(''); form_cart.append('');

var minus = form_cart.find($(".minus"));
var plus  = form_cart.find($(".plus"));

var qty = $(this).parent().find(".qty");

minus.on("click", function(){
  if (qty.val() <= 1) {
    qty.val(1);
  } else {
    qty.val((parseInt(qty.val())-1));
  }
});
plus.on("click", function(){
  qty.val((parseInt(qty.val())+1));
});

};

Ad
source: stackoverflow.com
Ad