I simply try to assign a background color to each div.
it looks simple but it doesn’t work.
var divElements = $("div");
var colorArray =["444", "344", "345", "472", "837", "043", "345", "472", "837", "043"];
for (var i=0; i< divElements.length; i++){
divElements[i].css("background","#"+colorArray[i])
}
I also tried to use jQuery’s each
$("div").each(function(i) {
divElements[i].css("background","#"+colorArray[i])
})
How do code this in javaScript generic “for loop” and jQuerys .each()
You have to watch out for exceeding the bounds of colorArray though (ie if you get too many divs). Possibilities include stopping when you hit the max:
or cycle through them:
Not sure why you’d want to do it as Javascript but: