0

How i can sum up these var ?

var curb = $("#nav-b").position().top;
var curc = $("#nav-c").position().top;
var curd = $("#nav-d").position().top;
var cure = $("#nav-e").position().top;
var curf = $("#nav-f").position().top;
var curg = $("#nav-g").position().top;
var curi = $("#nav-i").position().top;

I have tried something using this:

var let = '';
var charCodeRange = {
    start: 66,
    end: 90
} // b to z
for (var cc = charCodeRange.start; cc <= charCodeRange.end; cc++) {
    let = String.fromCharCode(cc);
    'cur' + let = $("#nav-" + let).position().top;
}
Sergio
  • 28,539
  • 11
  • 85
  • 132
zorg
  • 59
  • 1
  • 1
  • 8

1 Answers1

2

Why aren't you using classnames? You can just put the class of "letter" on the ones you need. Here's a quick example.

var total = 0;

$('.letter').each(function(){
  total += $(this).position().top;
});
Bill Criswell
  • 32,161
  • 7
  • 75
  • 66
  • I use them in functions after: if($("#nav-b").position().top < 20 && $("#nav-b").position().top > -20) { if(curb < $("#nav-b").position().top) changeNavIndicator("A"); else changeNavIndicator("B");; curb = $("#nav-b").position().top; } – zorg Sep 04 '13 at 16:48