0

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?

2
  • What color should get assigned? Your looping through all the colors so it sets the background equal to them in each iteration ending on the last color which is the final color set. Commented May 7, 2013 at 0:50
  • Yeah that is what is happening. I want each TR to have a different color and if there are more than (6) tr's it will go back to the beginning of the array. so basically each tr will have a different BG Commented May 7, 2013 at 0:51

1 Answer 1

1
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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Kevin Bowersox! so basically I only needed one for loop, and an if else statement? I just like to know how the code works for future reference and so I can actually know how to do this instead of just a code given to me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.