You want to iterate over all the elements of the type you want (in this case I selected a div with its super generic) and dynamically assign a color like this:
HTML:
<div id="one">Some text</div> <div id="two">Some text</div> <div id="three">Some text</div> <div id="four">Some text</div> <div id="change"> <input type="button" value="Change Colors!" /> </div>
JQuery
$("#change").click(function () { $("div").each(function () { var color = $(this).css("color"); if (color == "rgb(170, 170, 170)") { $(this).css("color", "#ccc"); } }); });
NOTE. JQuery .css() returns an RGB color value such as rgb (r, g, b), so you need to convert the returned RGB value to a HEX value. This can be done programmatically, but is beyond the scope of this question.
CSS
#one, #two { color: #aaa; } #three, #four { color: green; }
Here is the JSFiddle:
http://jsfiddle.net/hQkk3/44/
Phillip berger
source share