Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery append before final element

Tags:

jquery

I have html structured like so.

<div id='container'>   <div class='item'> ... </div>   <div class='item'> ... </div>   <div class='item'> ... </div>   ...   <div id='item_final'><input type="button" id='addOne'>...</div> </div> 

and what I am trying to do it add another item to the container class before the item_final div. The items are dynamic, so the number of them is unkown.

like image 848
GSto Avatar asked Aug 03 '10 13:08

GSto


People also ask

How do I add a div to my last child?

To insert element as a last child using jQuery, use the append() method. The append( content ) method appends content to the inside of every matched element.

What is prepend jQuery?

prepend() method inserts the specified content as the first child of each element in the jQuery collection (To insert it as the last child, use . append() ).

What is append and prepend in jQuery?

append() & . prepend() .append() puts data inside an element at the last index; while. . prepend() puts the prepending element at the first index.


2 Answers

$('#container div:last').before( $('<div>') ); 
like image 138
Stefan Kendall Avatar answered Sep 16 '22 16:09

Stefan Kendall


Try the .insertBefore method.

$('<div>').insertBefore('#item_final'); 
like image 24
David Hellsing Avatar answered Sep 20 '22 16:09

David Hellsing