Prevent calculation from being negative

1

Colleagues,

How would I do to make the calculation of the cart not negative. See below:

When I click the - button the quantity remains at 1, but the calculation continues to negative:

ThecodeI'musingis:

//subtrairquantidade$('.value-minus').on('click',function(){vardivUpd=$(this).parent().find('.value'),newVal=parseInt(divUpd.text(),10)-1;if(newVal>=1)divUpd.text(newVal);varvalorProduto=$(this).closest('tr').find('td[data-nome]').data('nome');//Funcionaltrocar=valorProduto.replace(",",".");
 valorTTotal =  document.getElementById("total").innerHTML;
 trocarSTotal = valorTTotal.replace(",",".");
 valorTotal = trocarSTotal - trocar;

if(result == '0.00'){ resultado = result; }else{ resultado = ''; }
 document.getElementById("total").innerHTML = valorTotal.toFixed(2) + resultado;
 document.getElementById("subtotal").innerHTML = valorTotal.toFixed(2);
 $.ajax({
     type: "GET",
     dataType: 'json',
      data:{ valor: valorTotal.toFixed(2) },
     url: "atualizar-carrinho.php",
     success: function(resposta){
     }
  });
});

HTML

<td class="invert">
<div class="quantity">
<div class="quantity-select">

<div class="entry value-minus">&nbsp;</div>
<div class="entry value"><span>1</span></div>
<div class="entry value-plus active">&nbsp;</div>
</div>
</div>
</td>
    
asked by anonymous 17.03.2017 / 22:28

1 answer

0

I managed to resolve. I created a conditional like this:

if(newVal >= 1){
     valorTotal = trocarSTotal - trocar;
}

newVal is the quantity.

    
18.03.2017 / 13:34