changecss() - Change any css class attributes


/ Published in: JavaScript
Save to your folder(s)

example usage:

changecss('.ClassName','width','280px');
changecss('#IDname','color','red');


Copy this code and paste it in your HTML
  1. function changecss(theClass,element,value) {
  2. var cssRules;
  3.  
  4. for (var S = 0; S < document.styleSheets.length; S++){
  5.  
  6.  
  7. try{
  8. document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
  9.  
  10. } catch(err){
  11. try{document.styleSheets[S].addRule(theClass,element+': '+value+';');
  12.  
  13. }catch(err){
  14.  
  15. try{
  16. if (document.styleSheets[S]['rules']) {
  17. cssRules = 'rules';
  18. } else if (document.styleSheets[S]['cssRules']) {
  19. cssRules = 'cssRules';
  20. } else {
  21. //no rules found... browser unknown
  22. }
  23.  
  24. for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
  25. if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
  26. if(document.styleSheets[S][cssRules][R].style[element]){
  27. document.styleSheets[S][cssRules][R].style[element] = value;
  28. break;
  29. }
  30. }
  31. }
  32. } catch (err){}
  33.  
  34.  
  35.  
  36. }
  37.  
  38. }
  39.  
  40.  
  41. }
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.