SO question 35381534

by Felipe Franco

HTML

<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<table class="table cart">
  <thead>
    <tr>

      <th class="cart-product-thumbnail">&nbsp;</th>
      <th class="cart-product-name">Produto</th>
      <th class="cart-product-quantity">Quantidade</th>
      <th class="cart-product-price">Precço Unitario</th>
      <th class="cart-product-subtotal">Total</th>
    </tr>
  </thead>
  <tbody>
    <tr class="cart_item">


      <td class="cart-product-thumbnail">

      </td>

      <td class="cart-product-name">
        <a href="#">Pera</a>
      </td>



      <td class="cart-product-quantity">
        <div class="quantity clearfix">
          <input type="button" value="-" class="minus" field="quantity">
          <input type="text" id="quantity" name="quantity" value="1" class="qty" />
          <input type="button" value="+" class="plus" field="quantity">
        </div>
      </td>
      <td class="cart-product-price">
        R$ <span id="price" class="amount">50000</span>
      </td>
      <td class="cart-product-subtotal">
        <span id="total" class="total_amount"></span>
      </td>
    </tr>
    <tr class="cart_item">


      <td class="cart-product-thumbnail">

      </td>

      <td class="cart-product-name">
        <a href="#">Uva</a>
      </td>



      <td class="cart-product-quantity">
        <div class="quantity clearfix">
          <input type="button" value="-" class="minus" field="quantity">
          <input type="text" id="quantity" name="quantity" value="1" class="qty" />
          <input type="button" value="+" class="plus" field="quantity">
        </div>
      </td>
      <td class="cart-product-price">
        R$ <span id="price" class="amount">40000</span>
      </td>

      <td class="cart-product-subtotal">
        <span id="total" class="total_amount"></span>
      </td>


    </tr>


    <tr class="cart_item">


      <td class="cart-product-thumbnail">

      </td>

      <td class="cart-product-name">
      ...

JavaScript

function calculate(obj) {

	var obj_price = $(obj).closest('.cart_item').find('.amount');
  var obj_total = $(obj).closest('.cart_item').find('.total_amount');

  var price = parseFloat( (obj_price.is("input") ? obj_price.val() : obj_price.text()) ) || 0;
  var quantity = parseInt($(obj).closest('.cart_item').find('.qty').val());
  var total = price * quantity;

	if (obj_price.is("input")) {
  	obj_total.val(total);
   } else {
   	obj_total.text(total);
   }
}

function changeQuantity(num, obj) {

  $(obj).parent().find('.qty').val(parseInt($(obj).parent().find('.qty').val()) + num);
}

$().ready(function() {
  //calculate();
  $(".minus").click(function() {
    changeQuantity(-1, this);
    calculate(this);
  });
  $(".plus").click(function() {
    changeQuantity(1, this);
    calculate(this);
  });


  $(".qty").keyup(function(e) {
    if (e.keyCode == 38) changeQuantity(1, this);
    if (e.keyCode == 40) changeQuantity(-1, this);
    calculate(this);
  });


});