Skip to content

Instantly share code, notes, and snippets.

@RStankov
Created March 14, 2009 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RStankov/79215 to your computer and use it in GitHub Desktop.
Save RStankov/79215 to your computer and use it in GitHub Desktop.
// used for setting css styles
// example:
// setCssRule('tag-name', {display: none});
function setCssRule(selector, styles){
var i, x, sheet, rules;
for (x = document.styleSheets.length - 1; 0 <= x ; x--){
sheet = document.styleSheets[x];
rules = sheet.cssRules || sheet.rules;
for (i = rules.length - 1; 0 <= i; i--){
if (rules[i].selectorText == selector){
return Object.extend(rules[i].style, styles);
}
}
}
i = rules.length;
if (sheet.insertRule){ // Normal browsers
sheet.insertRule(selector + '{ }', i);
} else { // IE: if (sheet.addRule){
sheet.addRule(selector, ';', i);
}
Object.extend((sheet.cssRules || sheet.rules)[i].style, styles);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment