How do you add CSS rules (eg strong { color: red }
) by use of Javascript?
Here's a slightly updated version of Chris Herring's solution, taking into account that you can use innerHTML
as well instead of a creating a new text node:
function insertCss( code ) {
var style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
// IE
style.styleSheet.cssText = code;
} else {
// Other browsers
style.innerHTML = code;
}
document.getElementsByTagName("head")[0].appendChild( style );
}