How To Override CSS With JavaScript

expand
Posted in: How To, 2010-08-31 09:56:04
by Alexander V. Røyne-Helgesen
Tagged as:
A pretty nice snippet that I use all the time to override CSS set with the !important switch.
function addNewStyle(newStyle) {
    var styleElement = document.getElementById('styles_js');
    if (!styleElement) {
	styleElement = document.createElement('style');
	styleElement.type = 'text/css';
	styleElement.id = 'styles_js';
	document.getElementsByTagName('head')[0].appendChild(styleElement);
    }
    styleElement.appendChild(document.createTextNode(newStyle));
}