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.

2
  • What do you need to get? Is it a style of an DOM element? Commented Aug 10, 2012 at 18:17
  • 5
    Have a look here and see if this solution will work for you: stackoverflow.com/questions/707565/… Commented Aug 10, 2012 at 18:17

1 Answer 1

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

document.body.appendChild(styleElement);

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

Sign up to request clarification or add additional context in comments.

2 Comments

I wanna GET the CSS codes in a style element. Not SET it. Not get a style element. Is there solution?
document.styleSheets[0].innerHTML

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.