In JavaScript, the className property gives the class name of an HTML element, but how to get the contents of the class.
-
Please revisit your other questions and accept the best answers on each of them (i.e. click the big check mark to the left of the best answers). It'll give you a 2-point reputation bump per question, and it helps others know what helped you, thus improving the overall quality of information on this site.FishBasketGordo– FishBasketGordo2011-07-31 17:35:44 +00:00Commented Jul 31, 2011 at 17:35
2 Answers
first you need to get the cssRules array but since it is not cross platform you should use a code like this:
if ( document.styleSheets[0].cssRules ) {
var cssRules = document.styleSheets[0].cssRules;
} else if ( document.styleSheets[0].rules ) {
var cssRules = document.styleSheets[0].rules;
}
now cssRules is an array of selectors in your stylesheet you can go through it like this:
for( var i = 0; i < cssRules.length; ++i ) {
if( cssRules[i].selectorText == '.myClass' ) {
alert( cssRules[i].style.color );
}
}
Comments
The actual style rules are stored in the style property.
Also, jQuery has some really nice methods for working with style. If you want to explore that route, check out the css method.
EDIT: Just re-read the question and realized that the OP was asking about the style rules for the class, not the style rules for the element. However, because the context isn't exactly clear, I'll let this answer stand for now, as it may still be helpful.
4 Comments
style property is the pure JavaScript way. You'll want to look at that then.