35

I'm trying to append an external html content (div plus some text inside just for testing) this way:

$.get("banner.html", function(data){
    $(this).children("div:first").append(data);
});

That seems to be pretty simple... but it does not seem to work. Any idea? Thanks!

xus
  • 2,587
  • 8
  • 31
  • 44

5 Answers5

60

Use html instead of append:

$.get("banner.html", function(data){
    $(this).children("div:first").html(data);
});
Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
erimerturk
  • 4,230
  • 25
  • 25
14

i'm not sure what you're expecting this to refer to in your example.. here's an alternative method:

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                $.get("banner.html", function (data) {
                    $("#appendToThis").append(data);
                });
            });
        </script>
    </head>
    <body>
        <div id="appendToThis"></div>
    </body>
</html>
Lloyd
  • 8,204
  • 2
  • 38
  • 53
3

You can use jquery's load function here.

$("#your_element_id").load("file_name.html");

If you need more info, here is the link.

Chamin Wickramarathna
  • 1,672
  • 2
  • 20
  • 34
0
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                $.get("banner.html", function (data) {
                    $("#appendToThis").append(data);
                });
            });
        </script>
    </head>
    <body>
        <div id="appendToThis"></div>
    </body>
</html>
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
qwertyqq
  • 1
  • 1
0

Use selectors like CSS3

$("banner.html>div:first-child").append(data);