Sem
Sem

Reputation: 4669

jQuery CSS appending with no effect

$('body').append('<style type="text/css"> body {background:red;} </style>');

This doesn't make the background red. Is there away outside the .css() function to do this? I get css, html and jQuery javascript seperatly through an ajax request so how do I append it the best way?


Ok I was trying to simplify the problem but this actually works.. so heres the whole code

edit : function(page) {
    $.ajax({ 
        data    : {
            'edit' : page
        },
        success : function(i){
            var html = jQuery.parseJSON(i);

            $('#editPageListContainer, #editPreview').remove();

            var script      = document.createElement('script');
            script.type     = 'text/javascript';
            script.text     = html.js;

            document.body.appendChild(script);

            $('body').append('<style type="text.css">' + html.css + '</style>' + html.php);
        }
    }); 
}

*OMG... a typo "text.css" in the style type... love this font it's so clear to see, love you guys anyway!*

Upvotes: 0

Views: 1618

Answers (4)

adeneo
adeneo

Reputation: 318182

$('body').attr('style', 'background:red');

Upvotes: 0

Tobias Bambullis
Tobias Bambullis

Reputation: 249

Add your CSS Code to the head tag, that works for me ;o)

$("head").append("<style> body {background:red;}</style>");

For your AJAX Request it looks like:

$.ajax({
  url: "url", 
  async:false, 
  success: function(css) {
     $("head").append("<style>"+css+"</style>");
  }
}); 

Upvotes: 1

Bas Slagter
Bas Slagter

Reputation: 9929

I think the best approach on this is to specify your styling in a stylesheet by specifying a class that you can use in your scripting. So....you have a css file with a class named 'myClass' and you add that class (with the addClass method) in your scripting at the moment you want. In my opinion this is the most clean approach.

Upvotes: 0

Riz
Riz

Reputation: 10246

$('body').css('background', 'red');

Upvotes: 4

Related Questions