tags:

views:

1183

answers:

3

i am inserting below css using jquery into head tag but problem is everytime i change the color using user interface it add another style tang and than css how can i remove already added and replace with new css please help.

<style charset="utf-8" type="text/css">div.contentMid2{background:#000;}div.module{background:#fff;}</style>

want to add couple of css on runtime but replacing with already added.

A: 

Can this website help you with your problem?

Natrium
thanks Notrium it can't help me out in my scenario
Yasir
A: 

$('div.contentMid2').css({background:'#000'})

aivarsak
i guess he want to change ALL css from that page without traversing all dom :)
Ionut Staicu
+1  A: 

if you need to change css values dynamically (background-color:VARIABLE), you can do one of this:

  • with ajax you request a new stylesheet with new values. After that, you inject it into head tag.
  • traverse all DOM every time you need to change something. $('.someDIv').css(). Be careful though, this way can be pretty resource eater :)

if you only need to add some static css, you can add/remove class from body:

$('body').addClass('new')

or

$('body').addClass('new2')

And from CSS:

.someClass {background:red}
body.new .someClass {background:blue}
body.new2 .someClass {background:green}
Ionut Staicu
thanks lonut what i am doing is on changing event i am saving css properties to database using ajax but to change layout i am inserting newly change or new css to head section but it uplicates because layout hav 3,4 elements and on every elment change i am creating css how delete first thn add new:(
Yasir
you don't need to delete. just add new stuff and the properties will be overwritten. Anyhow, on refresh, the extra code will be removed. so no ugly stuff will result :)
Ionut Staicu
i think you can gave a NAME or ID to <style> tag and then change their attributes :)
Ionut Staicu