Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

retriving property from style sheet

0 views
Skip to first unread message

alex bazan

unread,
Mar 21, 2005, 8:59:55 AM3/21/05
to
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.

Martin Honnen

unread,
Mar 21, 2005, 9:52:30 AM3/21/05
to

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/

alex bazan

unread,
Mar 21, 2005, 10:50:45 AM3/21/05
to
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/get-style-sheet-values-with-javascript/)

At last, i opted for a PHP solution.
thaks for all.
alex.

0 new messages