I’m trying to create 64 squares and give each a unique background color. I’m pretty close, but I can only seem to get one random color.
function randomCol() {
return Math.floor(Math.random()*16777215).toString(16);
}
$(function(){
for(i=0; i<64; i++) {
$('<div class="square"></div>').insertAfter(".starter");
$(".square").css({'background':'#' + randomCol()});
}
});
$(".square")selects all existing elements with classsquare, so you are assigning the same color to each element.You only want to assign the color to the just created one:
Two suggestions: