Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create child div in a 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 append a new div above the div id of lastChildDiv. Then how to do it?

like image 579
manishjangir Avatar asked Mar 13 '12 09:03

manishjangir


People also ask

What is the use of parent () and child () method in jQuery?

It is a jQuery Selector used to select all elements that are the direct child of its parent element. Parameter Values: parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.

Can we get the children element from the parent element using jQuery?

jQuery children() method is used to get the direct children of the selected HTML element. You can use children() method to traverse through the child elements of the selected parent element.

How do you get children of children in jQuery?

jQuery children() MethodThe children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.

What does parent () do in jQuery?

The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.


1 Answers

Use prepend

$('#main_div').prepend('<div id="new_div">...</div>');
like image 190
lowerkey Avatar answered Oct 21 '22 21:10

lowerkey