0

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()

3 Answers 3

4
$("div").each(function(i, val) {
  $(this).css("background", "#" + colorArray[i]);
});

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:

$("div").each(function(i, val) {
  if (i > colorArray.length) {
    return false; // breaks $.each() loop
  }
  $(this).css("background", "#" + colorArray[i]);
});

or cycle through them:

$("div").each(function(i, val) {
  $(this).css("background", "#" + colorArray[i & colorArray.length]);
});

Not sure why you'd want to do it as Javascript but:

var divElements = document.getElementsByTagName("div");
var colorArray =["444", "344", "345", "472", "837", "043", "345", "472", "837", "043"];
for (var i=0; i< divElements.length; i++){
  divElements[i].style.backgroundColor = '#' + colorArray[i];
}
Sign up to request clarification or add additional context in comments.

4 Comments

Great, how do i write this in generic js!
for a project that will sadly not use jQuery
Um, if it doesn't use jQuery, how are you getting $("div")?
it was just for my question purpose. Thanks anyway.
3

Here's what you do:

var colors = ["#444", "#344", etc.];

$("div").each(function (i)
{
    $(this).css("background-color", colors[i]);
});

You might get a small speed-gain if you skip converting "this" into a jQuery object and just use the JavaScript native API, like so:

this.style.backgroundColor = colors[i];

Also, you might want to assign a default color, if you've got more DIV-elements than entries in your array:

this.style.backgroundColor = colors[i] || "#fff";



The native approach:

var colors = ["#444", "#344", etc.],
    divElements = document.getElementsByTagName("div"),
    i = divElements.length;

while (i)
{
    // This is a reverse while-loop and runs a lot faster than other methods.
    // This means that the first div element will get assigned the value of the
    // last entry in the "colors"-array. If this breaks your brain, use the
    // Array.reverse() function on the "colors"-array declaration :)

    i--;

    divElements[i].style.backgroundColor = colors[i] || "#fff";
}

2 Comments

This answers the non-jQuery question well.
+1 for nice code even if it´s not answering the original question.
0

try background-color instead of background, which is the shorthand for multiple other CSS rules.

divElements[h].css("background-color","#"+colorArray[h])

Comments

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.