0

Possible Duplicate:
How to change/remove CSS classes definitions at runtime?

<!DOCTYPE html>
<meta charset="utf-8">
<title>An HTML5 Document</title>
<style>
    body {
        background: red;
    }
</style>

How can I get CSS codes in the style element via JS? Thank you.

Community
  • 1
  • 1
weilou
  • 4,419
  • 10
  • 43
  • 56

1 Answers1

0
var styleElement = document.createElement("style");
styleElement.innerHTML = "body { \n\
        background: red; \n\
    }";

document.body.appendChild(styleElement);

Fiddle: http://jsfiddle.net/maniator/zjrJ2/

Naftali
  • 144,921
  • 39
  • 244
  • 303