DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Reading Attribute Values From A Stylesheet

  • submit to reddit
        Used for reading a value from the loaded stylesheet. Only support the usage of one stylesheet file. Supports selectors beeing grouped and defined in multiple blocks.

function getCssValue(selector,attribute)
      selector = selector.toLowerCase();
      var stylesheet = document.styleSheets[0];
      var n = stylesheet.cssRules.length;
      for(var i=0; i<n; i++)
         var selectors = stylesheet.cssRules[i].selectorText.toLowerCase().split(",");
         var m = selectors.length;
         for(j=0; j<m; j++)
            if(selectors[j].trim() == selector)
               var value = stylesheet.cssRules[i].style.getPropertyValue(attribute);
                  return value;
      debug("Could not find selector/attribute: "+selector+"/"+attribute);
      return null;