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?
div
lastChildDiv
Use prepend
$('#main_div').prepend('<div id="new_div">...</div>');
Use before to add a newly created item:
$('#lastChildDiv').before('<div></div>');
You can use .before ()
var myNewDiv = '<div id='someId'></div>'; $('#main_div').html(myNewDiv);
or
var myNewDiv = '<div id='someId'></div>'; $('#main_div').append(myNewDiv);