Never been to DZone Snippets before?

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

Reading attribute values from a stylesheet (See related posts)

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);
               if(value!="")
               {
                  return value;
               }
            }
         }
      }
      debug("Could not find selector/attribute: "+selector+"/"+attribute);
      return null;
   },

You need to create an account or log in to post comments to this site.


Click here to browse all 5146 code snippets

Related Posts