Here's a fiddle showing you a couple ways you can do this.
This could work nicely for you
function total(jq, callback) {
return jq.get().reduce(function (sum, elem) {
return sum + window.parseInt(callback(elem), 10);
}, 0);
}
Then use it like this
total($(".price"), function(elem) {
return $(elem).text();
});
I think this approach is nicer because it gives you flexibility over the type of elements you could be total-ing.
For example, if you were trying to total <input> elements, you would want this instead
total($("input"), function(elem) {
return $(elem).val();
});
Even better, make it into a lightweight jQuery plugin like this
(function(window, $) {
function total(jq, callback) {
// code from above
}
$.fn.total = function(callback) {
return total(this, callback);
};
})(window, jQuery);
Now you can use it like this
$(".price").total(function(elem) {
return $(elem).text();
});