0

I want to push and plus value in array. I tried it but it is giving 'NaN'. I think it can be done by defining array type to integer. Can it is possible. Fiddle here.

var total=[];
$('.check').each(function(index, element) {
    $(this).find('div').each(function(index, element) {
        total[index]+= parseInt($(this).text())
    });
});

$('.update').find('div').each(function(index, element) {
    $(this).text(total[index])
});
2
  • Well you're using += on an empty array, so it's trying to take nothing and add to it, hence NaN Commented Sep 27, 2013 at 20:04
  • 2
    "Plus" is not a verb. Commented Sep 27, 2013 at 20:05

2 Answers 2

6

You're trying to add to nothing.

Here's one solution:

total[index] = (total[index] || 0) + parseInt($(this).text())

http://jsfiddle.net/yfC5Z/

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

Comments

4

Try this:

$(this).find('div').each(function(index, element) {
    if (total[index] == undefined) total[index] = 0;
    total[index]+= parseInt($(this).text())
});

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.