Add element to top of body in jQuery

Right now, Facebook wants me to select this ugly div at the top of my page right under <body>

 <div id="fb-root"></div> 

I would like to avoid this by adding it via JavaScript.

 .append('<div id="fb-root"></div>'); 

This puts it at the bottom of the page.

 </head> <body> <header>test</header> <div id="fb-root"></div> </body> 

How can I add it to the beginning?

 </head> <body> <div id="fb-root"></div> <header>test</header> </body> 
+8
javascript jquery
source share
2 answers

Use prepend method:

 $('body').prepend('<div id="fb-root"></div>'); 
+21
source share

you can use

 $('#fb-root').detach().insertBefore('body>*:first'); 
+1
source share

All Articles