0
$('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!*

2
  • Seems to work to me: jsfiddle.net/hZ2kK/2 Commented Oct 24, 2011 at 8:53
  • For future reference, always post the actual problematic code, not code "something like" it. Or, at least make sure your reduced code has the same problem. Commented Oct 24, 2011 at 9:12

4 Answers 4

4
$('body').css('background', 'red');
Sign up to request clarification or add additional context in comments.

4 Comments

What's reason not to use this ?
this should not be issue, using after loading ajax response.
i said OUTSIDE the .css() function.. i have a whole block of css i need to add
You can use {'attribute':'value', 'attribute':'value', 'attribute':'value'} in CSS function too.
1

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>");
  }
}); 

Comments

0

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.