0

I'm working on a form that calculates using multiplication and addition, but I can't seem to get the pieces to fall together correctly....

http://jsfiddle.net/cSfYE/

$('.qty').on('keyup', function() {
var sum = 0;
$("tbody tr").each(function(i,o){
    total = parseInt($(o).find(".qty").val(), 10) * parseInt($(o).find(".price-integer").val(), 10);
    if(!isNaN(total) && total.length!=0) {
        $(o).find(".install-total").val(total);
        sum += total;
    }
});
$("#total-equipment").val(sum);
});

When the user types in a quantity, say 2 in the first column, that should multiply by the figure in the fourth column (3,000) and be outputted into the Install Total column.

Then all the numbers in the Install Total column need to be added up and outputted in the Total Equipment Cost input field.

And finally, the + Installation figures need to add up.

And all this has to happen after the user enters a quantity, which that part works. Yay.


I can't seem to get the syntax right or something! I've been looking at stackoverlow.com's posts for the past few days and nothing is working, so I'm hoping someone could throw me a bone and help me out. I really do appreciate your time.

1
  • 1
    $(o).find(".qty").val() is one issue, the td doesn't have a value. Try $(o).find("input.qty").val(). You're making the same mistake when you get the price. Commented Oct 31, 2013 at 19:23

1 Answer 1

5

Problem rises in your selectors within row. You are referencing the <td> classes, not the inputs within them

This works, but might alos consider adding class to the inputs instead

 total = parseInt($(o).find(".qty input").val(), 10) * parseInt($(o).find(".price-integer input").val(), 10);

DEMO Updated

EDIT: addiitonal note - removed the test for length since integer doesn't have length

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

5 Comments

Thank you for that! I really appreciate it. It worked just fine. One more thing -- I'm trying to get the + Installation part to work. I updated the JS Fiddle: jsfiddle.net/cSfYE/1 So, if a quantity is filled out in the first three fields, then add up the Installation cost (which is a hidden input field). I thought what I did was right, but it's not working. Thoughts?
not seeing any hidden field...where is it? Also why can't you just add it in the if( !isNaN)
Hi Charlietfl, Sorry I just added it. jsfiddle.net/cSfYE/3 <input type="hidden" value="500" class="installation-quote" name="installation-quote">
Hi Charlietfl, it's a install per unit. I guess it would be the same code as the first one, but I'm not sure where to put it? Is "sum" a variable that I can change the name of?
a simple if to see if install in row if($(o).find(".installation-quote').length) then do math and add to 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.