views:

241

answers:

2

On a page I include some css eg:

<style type='test/css'>
.myBox{
color:#000000;
border:#FFFFFF;
padding:4px;
}
.myBox2{
color:#000000;
border:#FFFFFF;
padding:4px;
}
</style>

I want to then change the "color" property set within both myBox and myBox2 in jquery, without knowing the actual name (myBox, myBox2) of the css style. In otherwords, I want to update ALL css styles on the page where color = certain value, and then also update border where = certain value.

An example (not valid code) might be:

$("all css [color:#000000]").html("#FF0000");

How could this be possible using jquery?

+1  A: 

Perhaps you should look at refactoring your HTML + CSS. The entire point of CSS is to 'cascade' styles down through your site. So it would make awesome sense to have something like this declared:

body {color:#333;}

The above CSS property is inherited throughout all elements on your site. When it comes time to switch styles you could override the above style with your own:

$("body").css("color", "purple");

Or alternatively, maybe you want to have a look at a style sheet switcher, this gives you more fined grain control of your CSS (example), these style sheets could be generated at runtime, using a server side programming language (e.g. PHP) using a URL similiar to:

/css/generate-css.php?color=purple
wiifm
No because I need to target ALL elements with "color" = "purple" and switch it to "red". If "color" = "blue", then I don't want to affect it. In this way, users could customize all colors (and other css elements) on the entire website which is my goal. The body is not the only element that has a color property either.
Joe
I'm sure it's possible to change all elements on the page, with a specific css value, to another css value.
Joe
In this case, I would suggest the CSS style sheet switcher, this gives you the ability to have more fine grained control over your CSS
wiifm
I want to be able to update the css "LIVE", I don't have any pre-set stylesheets made up.
Joe
Just so we are clear, jQuery cannot select 'elements that are blue', api reference - http://api.jquery.com/category/selectors/. So basically this boils yours options down to this:- Give all 'changeable' areas that you want jQuery to affect an explicit class- Generate CSS on the fly using PHP or similar, and use a style sheet switcher- Refactor HTML + CSS so that is easier to select said changeable areas with jQuery
wiifm
The goal is for all css styles to be 'changeable'. I am generating the CSS via PHP already, however I don't have any ideas as to how to target these elements. Besides, I need to select the elements based on their color value. It must be do-able, even if it means working outside of jquery. Hopefully someone has a solution. Either way, I will keep looking.
Joe
+1  A: 

Not sure if this would work for you, since it is a completely different approach.

Rather than use the color css, use the class to target elements and add a new class that contains the color css.

For example

css

.color1 {color:#000000;}
.color2 {color:#FFFFFF;}
.color3 {color:#FF00FF;}

If you have full control of the html just add the color class in addition to the .mybox class. If you can't do that, something like $(".mybox").addClass("color1");

The css classes can also be dynamically changed, but I don't have a good example for the javascript to do that.

If you can, this is probably a better solution than finding items by their color attribute. If you were to try swapping colors say mybox is red and mybox2 is blue. To swap the colors you will need to temporarily set one color to a unique color. Otherwise when you set all red items to blue, mybox and mybox2 will both be blue and the set all blue items to red will change mybox back to red and mybox2 becomes red. Hope this helps.

Philip T.