Return to Snippet

Revision: 51312
at September 21, 2011 01:40 by uberdragon


Initial Code
function changecss(theClass,element,value) {
 var cssRules;

 for (var S = 0; S < document.styleSheets.length; S++){


	  try{
		document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);

	  } catch(err){
			try{document.styleSheets[S].addRule(theClass,element+': '+value+';');

			}catch(err){

				try{
					if (document.styleSheets[S]['rules']) {
					  cssRules = 'rules';
					 } else if (document.styleSheets[S]['cssRules']) {
					  cssRules = 'cssRules';
					 } else {
					  //no rules found... browser unknown
					 }

					  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
					   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
						if(document.styleSheets[S][cssRules][R].style[element]){
						document.styleSheets[S][cssRules][R].style[element] = value;
						break;
						}
					   }
					  }
				  } catch (err){}



			}

	  }


	}
}

Initial URL

                                

Initial Description
example usage: 

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

Initial Title
changecss() - Change any css class attributes

Initial Tags

                                

Initial Language
JavaScript