JSFiddle : http://jsfiddle.net/veksen/Wa4pA/4
what seems to be valid code in my eyes doesn’t work. I used a similar method earlier in the code and it worked fine, now my variable isn’t affected. I’m grabbing text values from html and summing them up into a variable. I’m looking for a hint on why this doesn’t work, and not a full working code.
Here is the code
$(document).ready(function() {
var char_fr = 0;
var char_cl = 0;
var char_lr = 0;
var char_pr = 0;
$(".ares .value").each( function(){
char_cr += Number($(this.text()));
char_fr += Number($(this.text()));
char_lr += Number($(this.text()));
char_pr += Number($(this.text()));
});
$(".cr .value").each( function() {
char_cr += Number($(this.text()));
});
$(".fr .value").each( function(){
char_fr += Number($(this.text()));
});
$(".lr .value").each( function(){
char_lr += Number($(this.text()));
});
$(".pr .value").each( function(){
char_pr += Number($(this.text()));
});
$("#mainstats .fr .stat").text(char_fr);
$("#mainstats .cr .stat").text(char_cr);
$("#mainstats .lr .stat").text(char_lr);
$("#mainstats .pr .stat").text(char_pr);
});
Again I’m looking for a lead on why it doesn’t work, instead of a full code. At the same time, I’ll be having a lot more variables with matching classes, would there be a smarter way to code this, rather than having a full block for each variable? The variables and classes are similar, but it would be just fine if they were an exact match, or maybe by “prefix_”+variable.
Thanks!
Not a jQuery wrapped DOM elements, use:
char_cr += Number($(this).text());Your version is:$(this.text())