I'm looking for a way to get a page's css with js without looking at a particular element, and then change the CSS rules rather than the CSS for different elements.
For instance, let say I want to change the background color of all elements that have a blue bg to red, and that after that some new elements might be created. I'd like to find all the css classes with blue bg and change them to red. document.styleSheets
doesn't seem to work that well:
For this page I tried this in the console. It doesn't log anything.
var logAttr = function(classes){
if(classes !== null){
for(var i = 0; i < classes.length; i++) {
for(var attr in classes[i].style){
console.log(attr);
}
}
}
};
for(var sheets_idx = 0; sheets_idx < document.styleSheets.length; sheets_idx++){
logAttr(document.styleSheets[sheets_idx].rules);
logAttr(document.styleSheets[sheets_idx].cssRules);
}
document.styleSheets[0].rules
and document.styleSheets[0].document.styleSheets
are null...