views:

41

answers:

1

I need to add a string before CSS elements with Javascript code. An example of the original code:

h1 {
   background: red;
}

.class #id {
   color: black;
}

I want Javascript to filter it like this:

.myclass h1 {
   background: red;
}

.myclass .class #id {
   color: black;
}

Let's say the CSS-code is a Javascript string called str.

+1  A: 

The best way to do this is to have the CSS already written and ready, and use javascript to add the needed class to the desired HTML elements.

Using jQuery you would do something like ("#id").addClass("myclass")

Where myclass is the name of the class which is used in your css.

You can dynamically add css manually using ("#id").css("...css...")

Zachary
I don't think it would work in my case. I have different CSS areas and I don't want to view all classes before the relevant stuff. h1 is also a default value in my CSS. I don't want to change all h1 tags.
Jens Törnell
You don't need to change all `h1` tags. You just need to define a CSS class and then apply it to some specific `h1`.
floatless