Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can jQuery load some html and append it to the <body> element?

Tags:

I tried the following:

$.load("Views/chatBox.html").appendTo('body') 

Console Output:

TypeError: $.load is not a function  

EDIT: The answer should only be one line of code; that's enough, I think.

like image 836
omg Avatar asked Sep 06 '09 13:09

omg


People also ask

How do you append something in jQuery?

jQuery append() MethodThe append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() 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.

How do you append html in html?

HTML code can be appended to a div using the insertAdjacentHTML() method. However, you need to select an element inside the div to add the code. This method takes two parameters: The position (in the document) where you want to insert the code ('afterbegin', 'beforebegin', 'afterend', 'beforeend')

Does jQuery append return the element?

append( function ) A function that returns an HTML string, DOM element(s), text node(s), or jQuery object to insert at the end of each element in the set of matched elements.


2 Answers

Nope, all those answers are incorrect because they rely on having a separate container!

Do this:

$.ajax({     url: "your.html",     success: function (data) { $('body').append(data); },     dataType: 'html' }); 
like image 170
Rob Evans Avatar answered Sep 24 '22 02:09

Rob Evans


I dont understand why placing container at the bottom of body and loading external page into that is not what you need?

What you can try is this:

<script type="text/javascript">     $(function() {         $("#container").load("Views/chatBox.html",function(){             $(this).clone().appendTo("body").remove();         });     }); </script> 

But im not 100% sure about this code... :)

like image 32
Gavrisimo Avatar answered Sep 23 '22 02:09

Gavrisimo