Skip to content
Advertisement

How can i give div background color based on count with differences of 3 within while loop

I have a div with several boxes within while loop. I want to give the two alternative div background color. For first 3 boxes i have to give the first color. Then for next 3 boxes i’v to give next color, then again for the next 3 boxes i’v to give the first color and so on.

Clearly i want to give the two colors alternatively for 3 boxes alternatively from first. Can anyone help me for this.

Advertisement

Answer

 var count=0;
 $("div").each(function (i) {
         count++;
        if (count%3) {
          this.style.color = "blue";
        } else {
          this.style.color = "black";
        }
      });
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement