0

The following question has been already asked Read CSS rule values with JavaScript? - Nov 27 '08.

I tried the same code on my browser google chrome 19.0.1084.46 and firefox 12.0 and the script does not work.

Is there a new script, library, maybe using jQuery, to active the same task on the most recent version of browsers today ?


Here the code which does not work:

function getStyle(className) {
    var classes = document.styleSheets[0].rules || document.styleSheets[0].cssRules
    for(var x=0;x<classes.length;x++) {
        if(classes[x].selectorText==className) {
                (classes[x].cssText) ? alert(classes[x].cssText) : alert(classes[x].style.cssText);
        }
    }
}
getStyle('.test')

Here a link to the jsfiddle

Community
  • 1
  • 1
underscore666
  • 1,719
  • 4
  • 24
  • 37
  • You may first try the solutions provided in this question http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element/5830517#5830517 – Prasenjit Kumar Nag May 17 '12 at 10:00
  • I checked this code on google chrome Version 20.0.1132.8 dev just now. and this seemed to work for me. – nightf0x May 17 '12 at 10:06
  • If the $("#elementToGetAllCSS") is not in the document but just in css file how can I get the rule? – underscore666 May 17 '12 at 10:06
  • go to this link, maybe I miss something. http://jsfiddle.net/9vjD2/ – underscore666 May 17 '12 at 10:09
  • well in your fiddle you are alerting a function and since your function does not return anything, the result of undefined is correct as for the alert inside of the function, if you remove the IF statement to check if the class is test then you will find out why it is not alerting. Don't worry it isn't that many alerts – Huangism May 18 '12 at 20:42

0 Answers0