Reputation:
I have following grade displayed dynamically
I want to calculate total grade after the data is loaded.
<div class="grade" id="grade-1">70</div>
<div class="grade" id="grade-2">90</div>
<div class="grade" id="grade-3">80</div>
<div class="grade" id="grade-4">60</div>
<div class="grade" id="grade-5">90</div>
<div class="grade" id="grade-6">80</div>
<div class="grade" id="grade-7">80</div>
<div class="grade" id="grade-8">60</div>
<div class="grade" id="grade-9">90</div>
.............
.............
using javascript I want to calculate this total, number of grades are dynamic. help me out.
Upvotes: 1
Views: 1021
Reputation: 29549
You can use javascripts reduce
function as an alternative as well:
var grades = document.querySelectorAll(".grade"),
grades = Array.prototype.slice.call(grades);
var total = grades.reduce(function(previous, current, index, array){
return parseInt(previous, 10) + parseInt(array[index].textContent, 10);
}, 0);
A few things of note here:
querySelectorAll
returns a nodelist, we must use Array.prototype.slice
to turn our results into a proper array.parseInt
. Example: http://jsfiddle.net/BTvsL/
As pointed out in the comments, you could do something similar by combining map
and reduce
:
var grades = document.querySelectorAll(".grade");
grades = Array.prototype.slice.call(grades);
var total = grades.map(function(el){
return parseInt(el.textContent, 10);
}).reduce(function(previous, current){
return previous + current;
}, 0);
Example 2: http://jsfiddle.net/BTvsL/1/
Note: If you need to test between .innerHTML
vs .innerText
given your browser requirements, you can do so by testing for each and defaulting to 0 like so:
var text = el.textContent || el.innerText || 0;
Upvotes: 2
Reputation: 339816
For browsers compatible with ES5 and the latest W3C specifications, you can get the sum all in one go:
var sum = $('div.grade').map(function() {
return +this.textContent || 0; // || 0 to cope with any NaNs
}).get().reduce(function(a, b) { return a + b }, 0);
i.e. get the elements, and the numeric value of their text content (compatible with IE9+), turn that into a native array and then use the ES5 standard function Array.prototype.reduce
to calculate the sum of those values.
Upvotes: 0
Reputation: 65264
another way to do is to use jQuery .map()
and javascript .reduce()
var total = $('.grade').map(function(){
return $(this).text()*1; // to make sure this is a number
}).get().reduce(function(a, b) {
return a + b;
});
alert(total);
Upvotes: 0
Reputation: 82241
You can use .each()
to loop through all elements :
var gradesum=0;
$('.grade').each(function(){gradesum+= parseInt($(this).text());});
alert(gradesum);
Upvotes: 2
Reputation: 9637
you can select the all element which is having .grade class .and sum the text
var total=0;
$(".grade").each(function(){
total+= +$(this).text();
});
alert(total);
Upvotes: 1
Reputation: 7372
var total = 0;
$("div.grade").each(function() { total += parseFloat($(this).html()); } );
Upvotes: 1
Reputation: 1325
You can do the following:
var total_grade = 0;
$('.grade').each(function() {
total_grade += parseInt($(this).text());
});
console.log(total_grade);
Hope this helps.
Upvotes: 2