Solved

JQuery question -- adding up all the values inside elements that have a specific class name?

Posted on 2012-04-11
4
340 Views
Last Modified: 2012-04-12
If I have an unordered list like the following ...

<ul id="budget_list">
   <li>Rent <span class="budget_item">1000</span></li>
   <li>Groceries <span class="budget_item">500</span></li>
   <li>Cable Bill <span class="budget_item">150</span></li>
</li>

Open in new window


... using JQuery -- how would I add up all the values that are inside elements that have the class name "budget_item" attached to them?

For example, .. I'd want to add up 1000 + 500 + 150, and yield the correct sum of "1650"

Is this possible to achieve using jquery?  If so, ... how?

Thanks,
- Yvan
0
Comment
Question by:egoselfaxis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 13

Accepted Solution

by:
haloexpertsexchange earned 167 total points
ID: 37834705
how about
var total = 0;
$('.budget_item').each(function(){

total = total  + $(this).text();
});
0
 
LVL 23

Assisted Solution

by:basicinstinct
basicinstinct earned 166 total points
ID: 37834799
A couple enhancements on halo's approach. I would always check it's numeric cos i am paranoid...



var total = 0;
$('.budget_item').each(function(){
  var next =  $(this).text();
  if($.isNumeric(next))
  {
     total = total  + parseInt(next);
  }
});
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 167 total points
ID: 37834813
var total = 0;
$('.budget_item').each(function(){

total = total  + parseInt($(this).text());
});

Open in new window

0
 

Author Closing Comment

by:egoselfaxis
ID: 37837259
Thanks guys!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question