0

I have some code, that to my mind, should be finding all classes '.stock-input', adding their values together, and displaying that value in the id #pStockQuantity, I can't see why I'm getting the result NaN.

JS:

$('.stock-input').keyup(function() {
    var stockTemp = parseInt(0);
    $('.stock-input').each(function() {
        stockTemp = parseInt(stockTemp) + parseInt($(this).val());
    });
    $('#pStockQuantity').val(stockTemp);
});
1
  • console.log(stockTemp) will tell you what you're getting. On a side note, since stockTemp is already an integer you can just do stockTemp += parseInt($(this).val()) Commented Sep 2, 2014 at 11:01

1 Answer 1

2

The problem will come if a textfield has a non numeric value

$('.stock-input').keyup(function () {
    var stockTemp = 0;
    $('.stock-input').each(function () {
        stockTemp += parseInt($(this).val()) || 0;
    });
    $('#pStockQuantity').val(stockTemp);
})

Demo: Fiddle

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

Comments

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.