Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add something after the "n" element using jQuery

Tags:

jquery

For a markup like

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

how do add some markup after the second DIV for instance?

like image 861
titel Avatar asked Nov 24 '09 18:11

titel


People also ask

How do you append something in jQuery?

jQuery append() MethodThe append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.

What is the difference between the append () and after () methods in jQuery?

. append() adds the parameter element inside the selector element's tag at the very end whereas the . after() adds the parameter element after the element's tag.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


1 Answers

Try the CSS selector :nth-child():

$("#holder > div:nth-child(2)").after("<div>foobar</div>"); 

See also the example on the jQuery page of the :nth-child() selector.

like image 52
Gumbo Avatar answered Oct 02 '22 15:10

Gumbo