I'm currently using a jQuery function to randomly assign one of a list of background colors to every element which has one of a list of CSS classes assigned to it:
$(document).ready(function() {
var colors = ['color1', 'color2', 'color3'];
$('.class1, .class2').each(function () {
var hue = colors[Math.floor(Math.random() * colors.length)];
$(this).css("background-color", hue);
});
});
I would like to alter the function so that instead of assigning the colors at random, it assigns them in sequence, looping back to the beginning of the color array when there are more elements to assign colors to than colors to assign, i.e.:
<element1 class="class1" style="background-color: color1;"></element1>
<element2 class="class2" style="background-color: color2;"></element2>
<element3 class="class1" style="background-color: color3;"></element3>
<element4 class="class2" style="background-color: color1;"></element4>
<element5 class="class1" style="background-color: color2;"></element5>
Any suggestions as to where to start with this would be very much appreciated!