9

Could someone tell me how I can modify the html before I insert it into the document?

This is an AJAX call:

url: "http://localhost/cart/public/admin/album",
success: function(html) {

This is the result of the AJAX call:

<div class="main-content slide-in">
    <h1>Create Album</h1>
    <div class="inner">
    </div>
</div>

All I want to do is change the color of the h1 tag. I have added this code

url: "http://localhost/cart/public/admin/album",
success: function(html) {
    $(html).find('h1').css('color','red');
    $('aside').after(html);

However this has no effect. The jQuery selector does seem to be working though.

url: "http://localhost/cart/public/admin/album",
success: function(html) {
    $(html).find('h1').css('color','red');
    console.log($(html).find('h1').length);
    $('aside').after(html);

Using console.log correct outputs 1. So it is finding the h1. For some reason though the css style isn't being applied.

I am bit stuck. Am I missing a step?

6
  • Are you trying to change the text color or the background color? Commented Oct 26, 2011 at 14:56
  • Can you make jsfiddle.net demo of this problem? Commented Oct 26, 2011 at 14:56
  • Your code should work, are you sure your page does not contain any rules to hide the elements you are adding? Commented Oct 26, 2011 at 15:03
  • 1
    Are you missing a dot before "aside"? Commented Oct 26, 2011 at 15:06
  • Is there any compelling reason not to add it to the DOM and then change its css? Commented Oct 26, 2011 at 15:46

4 Answers 4

17

Looks like you're changing the color and then adding the unchanged string to the DOM. Try this:

success: function(html) {
    var $html = $(html);
    $html.find('h1').css('color','red');
    $('aside').after($html);
Sign up to request clarification or add additional context in comments.

2 Comments

great answer :) it could be simplified as follows: $html.find('h1').css('color', 'red').end().insertAfter('aside')
Note: If you want the root element from the string, you can't use find on it, since the variable is the root element.
3

The accepted answer here actually wasted quite a lot of my time. At least it isn't generic. This is what figured out after sometime.

$.ajax({
  url: "http://localhost/~yoelusa/the-url-of-your-string.html",
  success: function(htmldata) {
    var $html = $("<div/>").append( htmldata );
    // make your changes on the html string, see some example bellow relevant to my htmlstring
    $html.find('#left').html("");
    $html.find('#right').html("");
    $html.find('.commentlist').remove();
    // when you are ready to insert your string somewhere call the html() function, in my example I inserted it in an iframe
    $("iframe").attr("srcdoc", $html.html() );
  }
});

Comments

2

Try this:

$(html).find('h1').css('color', 'red').parent().insertAfter('aside');

1 Comment

this would only insert the h1 and not the entire html element
0

Try adding !important to the css style:

$(html).find('h1').css("color", "red !important");

1 Comment

You can't use !important in a .css call.

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.