No...">

Create child div in parent div using jquery

I have the following div structure

<div id="main_div">
    <div id="lastChildDiv"></div>
</div>

Now I want to create or add a new one divabove the div id lastChildDiv. Then how to do it?

+5
source share
4 answers

Use prepend

$('#main_div').prepend('<div id="new_div">...</div>');
+10
source

Use before to add a newly created item:

$('#lastChildDiv').before('<div></div>');
+11
source

You can use .before ()

$('#lastChildDiv').before('<div></div>');
+2
source
    var myNewDiv = '<div id='someId'></div>';
    $('#main_div').html(myNewDiv);

or

    var myNewDiv = '<div id='someId'></div>';
    $('#main_div').append(myNewDiv);
0
source

All Articles