Solved

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

Posted on 2012-04-11
4
335 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

760 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now