Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / April 2005



Tip: Looking for answers? Try searching our database.

Changing CSS classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paweł - 30 Apr 2005 11:22 GMT
Is there any way I can dynamically (taking advantage of JavaScript) change
the properties for classes defined in CSS styles??
Let say there's a CSS class ".regtex" and it forces the browser to display
all elements of that class in red. Now I want to change this class so all
elements belonging to this class are displayed in blue, however I don't
want to change every single element, I just want to change the class'
properties. Is it possible??
Richard Cornford - 30 Apr 2005 12:52 GMT
Pawe³ wrote:
> Is there any way I can dynamically (taking advantage of
> JavaScript) change the properties for classes defined in
> CSS styles??

On dynamic visual browsers supporting the - document.styleSheets -
object (either in the Microsoft proprietary form or the W3C standard
form), yes.

> Let say there's a CSS class ".regtex"

Class selectors introduce some potentially unexpected issues as the -
selectorText - may be a normalised equivalent such as
"*[class~=regtext]" rather than the more expected, original -
selectorText - ".regtex", so some extra effort (indexOf or regular
expression) may be necessary to identify the correct cssRule.

> and it forces the browser to
> display all elements of that class in red.

Browsers cannot be forced to do anything, you just get to offer a
suggestion of color that they may (but mostly will) follow.

> Now I want to change this class so all elements
> belonging to this class are displayed in blue,
> however I don't want to change every single element, I
> just want to change the class' properties.

Assigning alternative values to the properties of the class's rule in
the style sheet object in the stylesheets collection will alter the CSS
acting on a page, in dynamic vi9sual browsers supporting the -
document.styleSheets - collection. Other factors may override the actual
presentation to the user.

> Is it possible??

Possible but not certain.

Richard.
Gus Richter - 30 Apr 2005 15:34 GMT
Pawel wrote:
> Is there any way I can dynamically (taking advantage of JavaScript) change
> the properties for classes defined in CSS styles??
[quoted text clipped - 3 lines]
> want to change every single element, I just want to change the class'
> properties. Is it possible??

One can access a collection of the style rules within a style sheet with
"cssRules" in order to alter any of its properties. Say your stylesheet
looked like this:

<style type="text/css">
a       {color:green;}
.regtex {color:red;}
</style>

Then one can alter anchors to orange with:
  document.stylesheets[0].cssRules[0].style.color. = "orange"
And one can alter regtex classed elements to blue with:
  document.stylesheets[0].cssRules[1].style.color. = "blue"

You may also wish to look into "selectorText" which is a string
containing the value of the selector, where in your case, selectorText
will contain ".regtex".

Another way is to call another class which will override previous
declarations. Here is an example which changes classes onmouseover:
http://www.home.golden.net/~richterf/Opera/ChangeClass_4_Opera_1.html
Disregard the fixed box which describes an Opera redraw bug.

Signature

Gus

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.