I have the following code:
<html>
<head>
<style id="ID_Style">
.myStyle
{
color : #FF0000 ;
}
</style>
</head>
<body>
<p class="myStyle">
Hello World !
</p>
</body>
</html>
And I want to modify the contents of <style>
through JavaScript.
The first solution was to use the innerHTML property of the style element (retrieved through its id), but while it works on Firefox, it fails on Internet Explorer 7.
So, I used pure DOM methods, that is, creating an element called style, a text node with the desired content, and append the text node as a child of the style node, etc. It fails, too.
According to MSDN, the <style>
element has an innerHTML
property, and according to W3C, the <style>
element is a HTMLStyleElement, which derives from HTMLElement, deriving from Element deriving from Node, which has the appendChild method. It seems to behave as if the content of a <style>
element was readonly on Internet Explorer.
So the question is: Is there a way to modify the content of a <style>
element on Internet Explorer?
While the current problem is with IE7, a cross-browser solution would be cool, if possible.
Style Element (MSDN): http://msdn.microsoft.com/en-us/library/ms535898.aspx
HTMLStyleElement (W3C): http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-16428977
You can use this test code if you want to reproduce your problem:
<html>
<head>
<style id="ID_Style">
.myStyle
{
color : #FF0000 ;
}
</style>
<script>
function replaceStyleViaDOM(p_strContent)
{
var oOld = document.getElementById("ID_Style") ;
var oParent = oOld.parentNode ;
oParent.removeChild(oOld) ;
var oNew = document.createElement("style") ;
oParent.appendChild(oNew) ;
oNew.setAttribute("id", "ID_Style") ;
var oText = document.createTextNode(p_strContent) ;
oNew.appendChild(oText) ;
}
function replaceStyleViaInnerHTML(p_strContent)
{
document.getElementById("ID_Style").innerHTML = p_strContent ;
}
</script>
<script>
function setRedViaDOM()
{
replaceStyleViaDOM("\n.myStyle { color : #FF0000 ; }\n")
}
function setRedViaInnerHTML()
{
replaceStyleViaInnerHTML("\n.myStyle { color : #FF0000 ; }\n")
}
function setBlueViaDOM()
{
replaceStyleViaDOM("\n.myStyle { color : #0000FF ; }\n")
}
function setBlueViaInnerHTML()
{
replaceStyleViaInnerHTML("\n.myStyle { color : #0000FF ; }\n")
}
function alertStyle()
{
alert("*******************\n" + document.getElementById("ID_Style").innerHTML + "\n*******************") ;
}
</script>
</head>
<body>
<div>
<button type="button" onclick="alertStyle()">alert Style</button>
<br />
<button type="button" onclick="setRedViaDOM()">set Red via DOM</button>
<button type="button" onclick="setRedViaDOM()">set Red via InnerHTML</button>
<br />
<button type="button" onclick="setBlueViaDOM()">set Blue via DOM</button>
<button type="button" onclick="setBlueViaInnerHTML()">set Blue via InnerHTML</button>
</div>
<p class="myStyle">
Hello World !
</p>
</body>
</html>
Thanks !
Note that moving the <style>
element from the <head>
into the <body>
doesn't change the problem.
The HTML DOM allows JavaScript to change the style of HTML elements.
To alter the style of HTML elements using JavaScript we use the HTML DOM style property. The HTML DOM style property allows you to get or set the style of an HTML element. There can be different approaches to use this property in order to alter the style of HTML elements.
Element Class Names Another way to alter the style of an element is by changing its class attribute. class is a reserved word in JavaScript, so in order to access the element's class, you use element. className .
Generating CSS on the fly has its advantages. If you would like to set the innerHTML of a style element in IE use styleSheet.cssText. For example: http://jsbin.com/awecu4
<!doctype html>
<script>
var style = document.createElement('style'),
script = document.getElementsByTagName('script')[0],
styles = '#test{background:green;}';
script.parentNode.insertBefore(style, script);
try{style.innerHTML = styles;}
//IE fix
catch(error){style.styleSheet.cssText = styles;}
</script>
<div id=test>Div with id of test</div>
Today, in all browsers (including I believe IE9+), you can set the value of textContent
on the style
element and it will work the way you want, including >
in selectors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With