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

retriving property from style sheet

P: n/a
Is it possible to retrieve a property from a defined style in a style sheet?

for example, if i have:

<style>
.mystyle {background-color:#ff0000}
</style>

how can retrieve the background color for that style?

is there something like: document.mystyle.background-color (?)

thanks in advance.
alex.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


alex bazan wrote:
Is it possible to retrieve a property from a defined style in a style
sheet?

for example, if i have:

<style>
.mystyle {background-color:#ff0000}
</style>

how can retrieve the background color for that style?


In Netscape 6 and later, Mozilla, IE 4 and later there is a property
document.styleSheets
thus if you have exactly one embedded CSS stylesheet
<style type="text/css">
.mystyle {background-color:#ff0000}
</style>
you can access it as
var stylesheet = document.styleSheets ? document.styleSheets[0] : null;
then in Mozilla and other W3C DOM compliant browsers each sheet has a
property cssRules for the rules, in IE there is a property rules e.g.
if (stylesheet) {
var rules = stylesheet.cssRules ? stylesheet.cssRules :
stylesheet.rules;
the first (and only rule in your example) has the index 0 e.g.
if (rules && rules[0]) {
rules[0].style.backgroundColor = '#00ff00';
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

P: n/a
En/na Martin Honnen ha escrit:

In Netscape 6 and later, Mozilla, IE 4 and later there is a property
document.styleSheets


Thanks, finally i got the right "googlequery" and found a script with a
similar code
(http://chapnickman.com/2005/03/14/ge...th-javascript/)

At last, i opted for a PHP solution.
thaks for all.
alex.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.