Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery append external html file into my page

Tags:

jquery

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!

like image 311
xus Avatar asked Nov 26 '11 09:11

xus


People also ask

How do I put an external HTML page into a div?

To load external HTML into a <div>, wrap your code inside the load() function. To load a page in div in jQuery, use the load() method.

How append HTML data to div in jQuery?

Add New HTML Contentappend() - Inserts content at the end of the selected elements. prepend() - Inserts content at the beginning of the selected elements. after() - Inserts content after the selected elements. before() - Inserts content before the selected elements.


3 Answers

Use html instead of append:

$.get("banner.html", function(data){
    $(this).children("div:first").html(data);
});
like image 69
erimerturk Avatar answered Oct 27 '22 09:10

erimerturk


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>
like image 20
Lloyd Avatar answered Oct 27 '22 11:10

Lloyd


You can use jquery's load function here.

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

If you need more info, here is the link.

like image 3
Chamin Wickramarathna Avatar answered Oct 27 '22 11:10

Chamin Wickramarathna