Dynamically add div to html page using javascript or jquery

I want to have a main div and be able to dynamically add new divs at the same level as the main div. Something like that:

<div id="main"></div>
<div id="created_div"></div>

Any help would be great

+5
source share
3 answers
$("#parent_div").append('<div id="created_div"></div>');

or if you want the newly created <div>-s to appear in front of others

$("#parent_div").prepend('<div id="created_div"></div>');
+7
source
$('#main').after('<div id="created_div"></div>');
+5
source
$('<div id="created_div"></div>').insertAfter('#main');
+2
source

All Articles