473,379 Members | 1,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Change class style

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
3 10625


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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete Kipe | last post by:
I'm not a JavaScript programmer...but I'm trying to put together a simple menu system for a new website and need a little help. I have the following script: <SCRIPT language=javascript> <!--...
4
by: Alexandre Jaquet | last post by:
Hi, I would like to know how can I change a row background color when an user select a row thx
10
by: Harry Haller | last post by:
How can I change the value in a css class? For instance, with I.E., I want to change the value in the class span.arr from middle to 0.2em. The value of middle is OK for most other browsers. I...
7
by: News | last post by:
I would like to know how I loop through a html file and validate it is the type of element I am seeking and then change something in the element. IE ...
1
by: sueian | last post by:
can some1 help with this code. can i add in a function like on click. when user click one of the link. the color will change to static from mouse over color. the text need to highligted after click....
7
by: moondaddy | last post by:
in asp.net 2.0, i have a link buttonand want to change the forecolor in a mouse over event. how can I do this? -- moondaddy@noemail.noemail
4
by: gregincolumbus | last post by:
I am trying to get the financial calculation on this to trigger whenever there is a change to select1. Right now, the user has to click on select2 to trigger the changes. Ideally, a change of...
4
by: TAL651 | last post by:
Hello, Here's the challenge I'm trying to solve: I'd like, when I click a button on a page, that it causes the style for all elements of a certain class to change. When I click the button...
15
by: Sunny | last post by:
Hi, I can change the lement opacity in IE using. abc.style.filter = 'alpha(opacity=' + 10 + ')'; But this dont work in firefox, In firefox it throws error. How I can change the opacity of an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.