EasyBB
EasyBB

Reputation: 6554

Looping background color with array and for loop

I am trying to loop the colors, and I am not getting this code correct. It is going to the last color. Not sure what I am doing wrong, I must be missing a line or something.

var forum = $('.main-content .statused tr'),i;
 var colors = ["#000","#F00","#FF0","#FFF","#0F0","#00F"];
    for(var j=0;j<forum.length;j++) {
       forumBG= forum[parseInt(j)];
       if(!forumBG) return;

  for (i=0;i<colors.length; i++){
   forum[j].style.background =colors[i];

  }
 }

Can anyone help me figure out the loop through different colored backgrounds?

Upvotes: 0

Views: 2027

Answers (1)

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

var forum = $('.main-content .statused tr'),i;
var colors = ["#000","#F00","#FF0","#FFF","#0F0","#00F"];
var i = 0;

 for(var j=0;j<forum.length;j++) {
     forumBG= forum[j];

     if(!forumBG) return;

     forum[j].style.background =colors[i];

     if(i == colors.length -1){
          i= 0;
     }else{
          i++;
     }
 }

Working Example http://jsfiddle.net/LMdXn/

Upvotes: 1

Related Questions