Using the same jQuery in multiple operations

I have a small question and I hope someone can help me: D

I am trying to create a product table where the user just adds the quantity of the product and jquery does the multiplication by its value and gives the result

I already did this and it works well:

    <script>
$(document).ready(function(){           
    $('#quantity_1').keyup(function(){   
    var price_1 = $("#price_1").val();
    var quantity_1 = $("#quantity_1").val();
    quantity_1 = quantity_1.replace(/[^0-9]+/g, '');
    $("#quantity_1").val(quantity_1);
    var total_1 = price_1 * quantity_1;
    $( "#total_1" ).val( total_1.toFixed(2) );
    });
});
</script>

        <table border="1" cellpadding="5px" cellspacing="0" >
        <tr>
        <td>Product</td>
        <td>Price</td>
        <td>Quantity</td>
        <td>Total</td>
      </tr>
    <tr>
        <td>Product Name</td>
        <td><input type="hidden" name="product[1][price]" id="price_1" value="10.00"  />10.00</td>
        <td><input type="text" name="product[1][quantity]" id="quantity_1"  /></td>
        <td><input type="text" name="product[1][total]" id="total_1" value="0" /></td>
      </tr>
    </table>

      

Working demo here: http://jsfiddle.net/EnterateNorte/9kswL0gf/

But I would like to be able to add multiple lines like:

<table border="1" cellpadding="5px" cellspacing="0" >
    <tr>
    <td>Product</td>
    <td>Price</td>
    <td>Quantity</td>
    <td>Total</td>
  </tr>
<tr>
    <td>Name 1</td>
    <td><input type="hidden" name="product[1][price]" id="price_1" value="10.00"  />10.00</td>
    <td><input type="text" name="product[1][quantity]" id="quantity_1"  /></td>
    <td><input type="text" name="product[1][total]" id="total_1" value="0" /></td>
  </tr>
<tr>
    <td>Name 5</td>
    <td><input type="hidden" name="product[5][price]" id="price_5" value="10.00"  />23.00</td>
    <td><input type="text" name="product[5][quantity]" id="quantity_5"  /></td>
    <td><input type="text" name="product[5][total]" id="total_5" value="0" /></td>
  </tr>
<tr>
    <td>Name 3</td>
    <td><input type="hidden" name="product[3][price]" id="price_3" value="130.00"  />10.00</td>
    <td><input type="text" name="product[3][quantity]" id="quantity_3"  /></td>
    <td><input type="text" name="product[3][total]" id="total_3" value="0" /></td>
  </tr>
<tr>
    <td>Name 4</td>
    <td><input type="hidden" name="product[4][price]" id="price_4" value="12.00"  />10.00</td>
    <td><input type="text" name="product[4][quantity]" id="quantity_4"  /></td>
    <td><input type="text" name="product[4][total]" id="total_4" value="0" /></td>
  </tr>
</table>

      

And if it's not too frustrating, I would be awesome if it SUM of all totals and shows the total at the end of the table :)

+3


source to share


5 answers


Using:

$(document).ready(function(){           
   $('[name*=quantity]').keyup(function(){   
     var price = $(this).parent().prev().find('input').val();
     var quantity = $(this).val();
     var total = price * quantity;
     $(this).parent().next().find('input').val( total.toFixed(2) );
});});

      

Working demo



Update: . To show Grand Total

    var sum = 0;
    //iterate through each textboxes and add the values
    $('[name*=total]').each(function() {

        //add only if the value is number
        if(!isNaN(this.value) && this.value.length!=0) {
            sum += parseInt(this.value);
        }

    });

      

Working demo

+2


source


What you can do is find elements using their relative position instead of hardcoded ids

$(document).ready(function () {
    $('input[id^="quantity_"]').keyup(function () {
        var $tr = $(this).closest('tr');
        var price = $tr.find('input[id^="price_"]').val();
        var quantity = this.value;
        var total = (price * quantity) || 0;
        $tr.find('input[id^="total_"]').val(total.toFixed(2));
    });
});

      



Demo: Fiddle

+1


source


I have updated your code on Fiddle . You need to change your markup a bit.

<tr>
    <td>Product Name</td>
    <td><input type="hidden" class="price" ... />10</td>
    <td><input type="text" class="quantity" ... /></td>
    <td><input type="text" class="total" ... /></td>
</tr>

$(document).ready(function(){           
    $('.quantity').keyup(function(){

        var parent = $(this).closest("tr");

        var price = $(".price", parent).val();
        var quantity = $(".quantity", parent).val();
        var total = price * quantity;

        $(".total", parent).val(total.toFixed(2));

    });
});

      

+1


source


I would recommend that you use the generic class

HTML:

<tr>
    <td>Name 5</td>
    <td>
        <input type="hidden" class="price" value="10.00" />10.00</td>
    <td>
        <input type="text" class="quantity" />
    </td>
    <td>
        <input type="text" class="total" value="0" />
    </td>
</tr>
<tr>
    <td>Name 3</td>
    <td>
        <input type="hidden" class="price" value="130.00" />130.00</td>
    <td>
        <input type="text" class="quantity" />
    </td>
    <td>
        <input type="text" class="total" value="0" />
    </td>
</tr>

      

Script:

$('.quantity').keyup(function () {
    var tr = $(this).closest('tr');
    var price = tr.find('.price').val();
    var quantity = $(this).val();
    var total = price * quantity;
    tr.find('.total').val(total.toFixed(2));
});

      

DEMO

+1


source


Modify the table:

<table border="1" cellpadding="5px" cellspacing="0" id='table' >
<!-- your rows with inputs -->
    <tr>
        <td>
            <input id='totalSum' value='0' type='text' />
        </td>
    </tr>
</table>

      

Do all your "summary" entries with just the addition of the "readonly" attribute.

js code:

$(document).ready(function(){           
    $('[name*=quantity]').keyup(function(){   
        var total = 0;
        $('#table tr').each(function(i,item){
            var price = parseFloat($(item).find('[name*=price]').val().replace(',','.'));
            var quantity = parseInt($(item).find('[name*=quantity]').val());
            if(!isNaN(price) && !isNan(quantity)){
                total += price*quantity;
            }
        });
        $("#totalSum").val(total.toFixed(2));
    });
});

      

+1


source







All Articles