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?
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.
. 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.
$() = 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With