1

In my code, when the button add is pressed, it creates a row in the table.

I need, in each line, when you leave the quantity or price input, the sum makes and assignes the value in the subtotal.

HTML

<table width="100%" border="0"  >
<tr>
    <td>Item</td>
    <td>Quantity</td>
    <td>U$ Price</td>
<td>Subtotal</td>
    <td>Add</td>
    <td>Remove</td>
</tr>
<tr class="tr_clone">
    <td>
        <select style="width:200px" name="itens[]">
        <option value="0"></option>
        <option value="1">Item A</option>
        <option value="2">Item B</option>
        <option value="3">Item C</option>
    </td>                                            
    <td><input type="text" size="5" maxlength="5" autofocus name="qtd[]" class="text ui-widget-content ui-corner-all"></td>
    <td><input type="text" size="10" maxlength="10" name="price[]" id="price" class="text ui-widget-content ui-corner-all"></td>
    <td><input type="text" size="10" maxlength="10" name="subtotal[]" class="text ui-widget-content ui-corner-all"></td>
    <td><input type="button" name="add" value="Add" class="tr_clone_add"></td>
    <td><input type="button" name="remove" value="Remove" class="tr_clone_remove"></td>
</tr>
</table>

jQuery

var $to_clone = $('.tr_clone').first().clone();

$("table").on('click', 'input.tr_clone_add', function () {
    var $tr    = $(this).closest('.tr_clone');
    var $clone = $to_clone.clone();
    $clone.find(':text').val('');
    $tr.after($clone);
});

$("table").on('click', 'input.tr_clone_remove', function () {
    var $tr    = $(this).closest('.tr_clone');
    $tr.remove();
});
3
  • 2
    What have you tried? You need to make an attempt, then tell us where you got stuck. Commented Mar 27, 2013 at 1:45
  • I can partly assist, try using document.getElementById to select the quantity and price fields for calculations. DEMO: jsfiddle.net/sDMjT I'm not sure how to make it detect which row you're working in so it will only work for the first row. Commented Mar 27, 2013 at 2:17
  • I really don't know how code I have to try, if focusin or focusout, each. I learning jquery, and I'm a little beat lost. Commented Mar 27, 2013 at 2:22

2 Answers 2

1

Try this. Should be what your looking for (fiddle)[http://jsfiddle.net/C6Su2/]:

HTML:

<table width="100%" border="0"  >
<tr>
    <td>Item</td>
    <td>Quantity</td>
    <td>U$ Price</td>
<td>Subtotal</td>
    <td>Add</td>
    <td>Remove</td>
</tr>
<tr class="tr_clone">
    <td>
        <select style="width:200px" name="itens[]">
        <option value="0"></option>
        <option value="1">Item A</option>
        <option value="2">Item B</option>
        <option value="3">Item C</option>
    </td>                                            
    <td><input type="text" size="5" maxlength="5" name="qtd" class="quantity text ui-widget-content ui-corner-all"></td>
    <td><input type="text" size="10" maxlength="10" name="price" class="price text ui-widget-content ui-corner-all"></td>
    <td><input type="text" size="10" maxlength="10" name="subtotal" class="subtotal text ui-widget-content ui-corner-all"></td>
    <td><input type="button" name="add" value="Add" class="tr_clone_add"></td>
    <td><input type="button" name="remove" value="Remove" class="tr_clone_remove"></td>
</tr>
</table>

jQuery 1.3+ (jsfiddle):

    var $to_clone = $('.tr_clone').first().clone();

$("table").on('click', 'input.tr_clone_add', function () {
    var $tr    = $(this).closest('.tr_clone');
    var $clone = $to_clone.clone();
    $clone.find(':text').val('');
    $tr.after($clone);
});

$("table").on('click', 'input.tr_clone_remove', function () {
    var $tr    = $(this).closest('.tr_clone');
    $tr.remove();
});
$(".quantity, .price").live("keyup", function(){
    var prtCont = $(this).parent().parent();
    var prce = parseInt(prtCont.find('.price').val()) - 0;
    var qnty = parseInt(prtCont.find('.quantity').val()) - 0;
    if(!isNaN(prce) && !isNaN(qnty)){
        prtCont.find('.subtotal').val(prce * (qnty));
    }else{
        prtCont.find('.subtotal').val("");
    }
});

OR jQuery 1.9+ (jsfiddle)

var $to_clone = $('.tr_clone').first().clone();

$("table").on('click', 'input.tr_clone_add', function () {
    var $tr    = $(this).closest('.tr_clone');
    var $clone = $to_clone.clone();
    $clone.find(':text').val('');
    $tr.after($clone);
});

$("table").on('click', 'input.tr_clone_remove', function () {
    var $tr    = $(this).closest('.tr_clone');
    $tr.remove();
});

$(document).on("keyup", ".quantity, .price", function(){
    var prtCont = $(this).parent().parent();
    var prce = parseInt(prtCont.find('.price').val()) - 0;
    var qnty = parseInt(prtCont.find('.quantity').val()) - 0;
    if(!isNaN(prce) && !isNaN(qnty)){
        prtCont.find('.subtotal').val(prce * (qnty));
    }else{
        prtCont.find('.subtotal').val("");
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your code it is very useful to me. can you explain how to sum all subtotal.
0

If you can make the following changes to the html try the below code

  1. Add the class qty to the input element qtd
  2. Add the class price to the input element price
  3. Add the class subtotal to the input element subtotal

Code

$('table').on('change', '.qty, .price', function(){
    var tr = $(this).closest('tr');
    var prce = parseInt($('.price', tr).val(), 10);
    var qnty = parseInt($('.qty', tr).val(), 10);
    if(!isNaN(prce) && !isNaN(qnty)){
        $('.subtotal', tr).val(prce * (qnty));
    }else{
        $('.subtotal',tr).val("");
    }
})

Demo: Fiddle

1 Comment

@MLewisCodeSolutions looks like you edited your answer after my post with jquery 1.9 solution not the other way around

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.