Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending HTML to end of body using javascript

I have several templates for faceboxes (lightbox) that I need at different points of the application. These are stored in different partials and files.

I will initialize different javascript functions in accordance to which ones I need. The question is, what is the best way to append the external HTML page into my body using javascript?

like image 362
meow Avatar asked Mar 21 '10 21:03

meow


People also ask

Can you append to innerHTML in JavaScript?

Using the innerHTML attribute: To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.

What is append to body?

append() is a method that adds (an) additional element(s) to the end of the selected parent element. $('body').append('<div>Appended to BODY</div>'); Before: <body> </body> After: <body> <div>Appended to BODY</div> </body>


1 Answers

Since you tagged the question with it, here's a jQuery solution.

$("body").append("text");

Remember that the parameter can also be a DOM element. So you can do this :

var p = $("<p/>").text("a paragraph");
$("body").append(p);
like image 185
Çağdaş Tekin Avatar answered Sep 25 '22 11:09

Çağdaş Tekin