Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the content of a <style> element through JavaScript

The Problem

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 Expected Solution

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.

The Question

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.

Appendix

Sources:

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

Complete Test Code

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 !

Edit

Note that moving the <style> element from the <head> into the <body> doesn't change the problem.

like image 756
paercebal Avatar asked Apr 23 '10 11:04

paercebal


People also ask

Can JavaScript change the style of an element?

The HTML DOM allows JavaScript to change the style of HTML elements.

How JavaScript can change the HTML style attributes and 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.

How can the style of an element be modified?

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 .


2 Answers

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>
like image 163
Lime Avatar answered Oct 11 '22 23:10

Lime


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.

like image 23
Stuart P. Bentley Avatar answered Oct 11 '22 22:10

Stuart P. Bentley