0

I have a dynamic form with the following fields

<tr>
 <td><input name = "qty[]" /></td>
 <td><input name = "color[]" /></td>
 <td><input name = "price[]" /></td>
 <td><input name = "total[]" /></td>
</tr>

I can dynamically add as many rows as I want. What I would like to accomplish is having total = qty*price for each row with a click of a button. Any suggestions?

1 Answer 1

3

Modified your HTML a bit

<tr>
 <td><input name = "qty[]" /></td>
 <td><input name = "color[]" /></td>
 <td><input name = "price[]" /></td>
 <td><input name = "total[]" /></td>
 <td><input type='text' name='total' /></td>
</tr>

jQuery code

$("#button1").click(function(){
    var trElems = $("#tab tr");

    trElems.each(function(){
        var qty = $(this).find("input[name='qty[]']").val();
        var price = $(this).find("input[name='price[]']").val();
        var total = parseFloat (qty) * parseFloat (price);

        $(this).find("input[name='total']").val(total);
    });

See a working demo

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. This is working exactly the way I want. Curious, why did you add the second "total"?
oops, I din see your total.:)

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.