Posted By

roblynch on 10/12/10


Tagged


Versions (?)

Change a CSS rule directly as if you were editing the css file from javascript.


 / Published in: JavaScript
 

This was useful today, could be made into something with a little work.

  1. function cssRule(selector,attribute,value,replaceAll)
  2. {
  3. if(replaceAll == null)
  4. {
  5. replaceAll = false;
  6. }
  7.  
  8. var ss = document.styleSheets;
  9. var count = 0;
  10. for (var i=0; i<ss.length; i++) {
  11. var rules = ss[i].cssRules || ss[i].rules;
  12.  
  13. for (var j=0; j<rules.length; j++) {
  14. if (rules[j].selectorText === selector) {
  15. rules[j].style[attribute] = value;
  16. if(replaceAll)
  17. {
  18. count++;
  19. }
  20. else
  21. {
  22. return true;
  23. }
  24. }
  25. }
  26. }
  27. return count;
  28. }

Report this snippet  

You need to login to post a comment.