Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append element after first child div in jquery?

Tags:

jquery

Assuming I have the following divs:

<div id="mydiv">
 <div>1</div>
 <div>2</div>
 <div>3</div>
 <div>4</div>
</div>

How can I in jquery or javascript make it such that I can append an element right after the first child of "mydiv". Or is there some selector to allow me to select and append after the first child?

<div>
 <div>1</div>
 <div>Place to Insert Subsequent div</div>
 <div>2</div>
 <div>3</div>
 <div>4</div>
</div>
like image 695
Rolando Avatar asked Jul 06 '12 01:07

Rolando


People also ask

How do I append a div after another div?

JavaScript – Insert Element after Specific Div Element To insert element in document after div element using JavaScript, get reference to the div element; call after() method on this div element; and pass the element to insert, as argument to after() method.

How do I add a div to my first child?

To insert element as a first child using jQuery, use the prepend() method. The prepend( content ) method prepends content to the inside of every matched element.

How do I append after an element?

First, select the ul element by its id ( menu ) using the getElementById() method. Second, create a new list item using the createElement() method. Third, use the insertAfter () method to insert a list item element after the last list item element.

Which is the correct syntax to insert content after the div elements?

The jQuery after() method is used to insert content after the selected elements.


Video Answer


1 Answers

You can do this using :first-child and after:

$("#mydiv div:first-child").after(newDiv);

Demo

like image 73
Tim Cooper Avatar answered Sep 24 '22 20:09

Tim Cooper