By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

Change class style

P: n/a
Hi,

Is it possible to dynamically change the style of a class instead of
just an individual element? If so could you point me in the right direction?

Robert
Jun 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a


Robert wrote:
Is it possible to dynamically change the style of a class instead of
just an individual element? If so could you point me in the right
direction?


You can change the style(s) of a certain rule in a stylesheet with IE
and with Netscape/Mozilla/Firefox. Opera however does not support that,
not sure about latest Safari/Konqueror.
There is a collection
document.styleSheets
for all stylesheets used (<style type="text/css"> element and <link
rel="stylesheet">).
With IE/Win each stylesheet (e.g.
document.styleSheets[someIndex]
) has a collection
document.styleSheets[someIndex].rules
with Netscape/Mozilla/Firefox the name of the collection is W3C DOM
Level 2 compliant and is
document.styleSheets[someIndex].cssRules
You can loop through those rules and find the rule by its selectorText
and then change properties defined in the rule e.g.
document.styleSheets[someIndex].cssRules[1].style.backgroundColor =
'blue';

Finally an inefficient way to change a class style is to add a new
stylesheet as the last stylesheet. With Opera that is your only way e.g.
var styleElement = document.createElement('style');
styleElement.type = 'text/css';
styleElement.appendChild(document.createTextNode(
'.className { background-color: blue }'
));
document.getElementsByTagName('head')[0].appendChild(styleElement);
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 9 '06 #2

P: n/a


Martin Honnen wrote:
You can change the style(s) of a certain rule in a stylesheet with IE
and with Netscape/Mozilla/Firefox. Opera however does not support that,


It looks as if Opera 9 is going to support document.styleSheets as the
latest weekly Opera 9 beta build
<http://my.opera.com/desktopteam/blog/show.dml/291397>
has that feature.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 10 '06 #3

P: n/a
Martin Honnen wrote:


Martin Honnen wrote:
You can change the style(s) of a certain rule in a stylesheet with IE
and with Netscape/Mozilla/Firefox. Opera however does not support that,

It looks as if Opera 9 is going to support document.styleSheets as the
latest weekly Opera 9 beta build
<http://my.opera.com/desktopteam/blog/show.dml/291397>
has that feature.


Okay, thanks!
Jun 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.