1

I have the child count coming from the server, i'd rather not do any more math there. I'd like to extract the number from the markup.

Within every .active-parent I need to loop through, and get a number from .category-count .

      children_counts = $('.active-parent .category-count')
      children_counts.each(function(){
        var parent_count = $(this).text();
        console.log(parent_count)
      });

I have it returning the numbers with the log statement

189
5
86
261

Can't figure out how to sum those up. Any help would be much appreciated.

1 Answer 1

2

Initialize a variable and use parseInt to add the count inside the each loop.

children_counts = $('.active-parent .category-count');
var totalCount = 0;
children_counts.each(function(){
   var parent_count = $(this).text();
   totalCount += parseInt(parent_count, 10);
});
console.log('Total Count is -- ' + totalCount );
Sign up to request clarification or add additional context in comments.

2 Comments

AMAZING. Thanks for the quick answer, now I just need to figure out how to append that to the .active parent ! Thanks
@jahrichie .. Glad to have helped... Just use append method .. $('.active-parent').append( $('<p/>', { text : total_count }));

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.