Davor Zubak
Davor Zubak

Reputation: 4746

How to dynamically change color of element?

How can I change colors of elements using variables?

Let's say we have four strings(colors). I need to give every class element different one, 1,2,3,4,1,2,3... and so on:

function pressLineColors() {

    var a = "#eee",
    b = "#123",
    c = "#fff",
    d = "#ae23e5";

     $('.pressLine').each(function (i) {
         //go througt each of this, and give them colors, so when new elements
        // appear, give them next color. 
     });
}

First element sholud have color a, second b, third c, fourth d, and fifth a, ...

Upvotes: 3

Views: 4984

Answers (6)

Damen TheSifter
Damen TheSifter

Reputation: 911

you need to put your list into an array-like object then access by index;

function pressLineColors() {

var colors = [ a = "#eee",
b = "#123",
c = "#fff",
d = "#ae23e5"];

 $('.pressLine').each(function (i) {
     //go througt each of this, and give them colors, so when new elements
    // appear, give them next color. 

   $(this).css("color" , colors[i]);
 });
}

Upvotes: 1

charlietfl
charlietfl

Reputation: 171679

Creating a variable to keep track , and a separate function will allow you to add elements later and keep track

/* colors in indexable array*/
var colors=["#eee", "#123","#fff","#ae23e5"];

var counter=0;
$('.pressLine').each(function () {
         $(this).css('color', colors[nextColorIndex()]);

});

function nextColorIndex(){
    var ctr=counter;
         counter=(++counter <colors.length) ? counter++ :0
         return ctr;
}

Upvotes: 2

Jasper
Jasper

Reputation: 75993

function pressLineColors() {

    //setup array of colors and a variable to store the current index
    var colors = ["#eee", "#123", "#fff", "#ae23e5"],
        curr   = 0;

    //loop through each of the selected elements
    $.each($('.pressLine'), function (index, element) {

        //change the color of this element
        $(this).css('color', colors[curr]);

        //increment the current index
        curr++;

        //if the next index is greater than then number of colors then reset to zero
        if (curr == colors.length) {
            curr = 0;
        }
    });
}

Here is a demo: http://jsfiddle.net/SngJK/

Update

You can also use the suggestion in the comments to this answer to shorten the code a bit:

function pressLineColors() {
    var colors = ["#eee", "#123", "#fff", "#ae23e5"],
        len    = colors.length;
    $.each($('.pressLine'), function (index, element) {
        $(this).css('color', colors[index % len]);
    });
}

Here is a demo: http://jsfiddle.net/SngJK/2/

Update

You can also use .css('color', function (){}) to iterate through each of the elements, returning the color you want to make the element:

$('.pressLine').css('color', function (index, style) {
    return colors[index % len];
});

Here is a demo: http://jsfiddle.net/SngJK/4/

Upvotes: 7

vireshas
vireshas

Reputation: 816

have these colors in an array

function pressLineColors() {

a = new Array();

a[0] = "#eee",
a[1] = "#123",
a[2] = "#fff",
a[3] = "#ae23e5";

var c = 0;
var size = a.length;

 $('.pressLine').each(function (i) {

      this.style.color = a[c];
      c++;
      if(c > size) c = 0;
  });
}

Upvotes: 3

Kyle
Kyle

Reputation: 22258

function pressLineColors() {
  var a = ["#eee","#123","#fff","#ae23e5"];

  $('.pressLine').each(function (i, ele) {
    $(ele).css("color", a[i % a.length]);
  });
}

Upvotes: 5

zod
zod

Reputation: 12417

use addClass

http://api.jquery.com/addClass/

or

http://api.jquery.com/css/

Upvotes: 1

Related Questions