7

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

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

Any help would be wonderful

3 Answers 3

8
$("#parent_div").append('<div id="created_div"></div>');

or if you want the newly created <div>-s to appear before the others

$("#parent_div").prepend('<div id="created_div"></div>');
Sign up to request clarification or add additional context in comments.

2 Comments

Assuming the original <div> was a child of <body>.
@JonathanM Correct. That was an assumption
6
$('#main').after('<div id="created_div"></div>');

2 Comments

This works universally, regardless of what level the original <div> is on.
There is another function insertAfter. But after performs better than insertAfter
4
$('<div id="created_div"></div>').insertAfter('#main');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.