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, …
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:
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:Here is a demo: http://jsfiddle.net/SngJK/4/