retriving property from style sheet

A

alex bazan

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.
 
M

Martin Honnen

alex said:
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';
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top