3

Using jquery, I want to loop all elements having the class "item" and apply different background colors according to the index of the element.

mapcolor is an array of colors (length = number of elements having "item" class)

$.each($(".item"), function(i,e){
$("#"+e).css("background-color",mapcolor[i]);
});

$("#"+e) selector doesn't work as expected, neither $("#"+e.id) ... Something's wrong with my selector. Any idea?

4
  • Try $(this).css("background-color",mapcolor[i]);. I think this will work if not please let me know about your HTML DOM structure. Commented Aug 19, 2015 at 10:33
  • 2
    better to use $(selector).each() method instead. Commented Aug 19, 2015 at 10:34
  • Thank you for your help.Actually every span is colored with the same color in my programm and it doesn't work. The issue must be elsewhere... I'm trying to figure out where. Commented Aug 19, 2015 at 11:03
  • Ok i figured out where was the issue. It was in another part of my programm and not in that "each loop". Thank you guys. Commented Aug 19, 2015 at 11:20

3 Answers 3

5

use .each() method instead and you have to be in the context with $(this):

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

Yet a better way is:

$(".item").css("background-color",function(){
    return mapcolor[$(this).index()];
});

make use of .css() method and pass a callback function to return the value.

A test is below:

var mapcolor = ['green', 'red', 'yellow'];

$(".item").css("background", function() {
  return mapcolor[$(this).index()];
});
div{height:10px;}
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

3 Comments

I've already tried that. Doesn't work.. It applies the same color (last one) to all elements
@Abspirit check a test snipped i have added.
@Abspirit the code snippet you have thrown here that led us to answer this so you have to ensure that this line is not in any other loop.
3

yes problem is in your selector ... problem is on this like

$("#"+e).css("background-color",mapcolor[i]);

why you using # with e because # represent id .. but here e is representing current selected tag in loop

so simply use $(e).css("background-color",mapcolor[i]); in your way..

or short and better way use this

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

Comments

0

Try something like this:

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

3 Comments

I've already tried that. Doesn't work.. It applies the same color (last one) to all elements
Can you please provide your HTML structure. So that I can see the exact issue.
If possible add to a jsfiddle. It's easier to see the exact issue.

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.